Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in Router::handle() #16431

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
## [5.3.1](https://github.com/phalcon/cphalcon/releases/tag/v5.3.1) (xxxx-xx-xx)

### Fixed
- Infinite save loop in Model::save() [#16395](https://github.com/phalcon/cphalcon/issues/16395)
- Infinite save loop in `Phalcon\Mvc\Model::save()` [#16395](https://github.com/phalcon/cphalcon/issues/16395)
- Undefined column with columnMap and model caching [#16420] (https://github.com/phalcon/cphalcon/issues/16420)
- Fixed memory leak in `Phalcon\Mvc\Router::handle()` [#16431] (https://github.com/phalcon/cphalcon/pull/16431)


## [5.3.0](https://github.com/phalcon/cphalcon/releases/tag/v5.3.0) (2023-08-15)
Expand Down
44 changes: 13 additions & 31 deletions phalcon/Mvc/Router.zep
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,7 @@ class Router extends AbstractInjectionAware implements RouterInterface, EventsAw
let handledUri = "/";
}

let request = null,
currentHostName = null,
let currentHostName = null,
routeFound = false,
parts = [],
params = [],
Expand All @@ -711,11 +710,22 @@ class Router extends AbstractInjectionAware implements RouterInterface, EventsAw
this->matchedRoute = null;

let eventsManager = this->eventsManager;

if eventsManager !== null {
eventsManager->fire("router:beforeCheckRoutes", this);
}

/**
* Retrieve the request service from the container
*/
let container = <DiInterface> this->container;
if container === null {
throw new Exception(
"A dependency injection container is required to access the 'request' service"
);
}

let request = <RequestInterface> container->get("request");

/**
* Routes are traversed in reversed order
*/
Expand All @@ -728,20 +738,6 @@ class Router extends AbstractInjectionAware implements RouterInterface, EventsAw
*/
let methods = route->getHttpMethods();
if methods !== null {
/**
* Retrieve the request service from the container
*/
if request === null {
let container = <DiInterface> this->container;
if container === null {
throw new Exception(
"A dependency injection container is required to access the 'request' service"
);
}

let request = <RequestInterface> container->getShared("request");
}

/**
* Check if the current method is allowed by the route
*/
Expand All @@ -755,20 +751,6 @@ class Router extends AbstractInjectionAware implements RouterInterface, EventsAw
*/
let hostname = route->getHostName();
if hostname !== null {
/**
* Retrieve the request service from the container
*/
if request === null {
let container = <DiInterface> this->container;
if container === null {
throw new Exception(
"A dependency injection container is required to access the 'request' service"
);
}

let request = <RequestInterface> container->getShared("request");
}

/**
* Check if the current hostname is the same as the route
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/Mvc/Router/HandleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function mvcRouterHandle(IntegrationTester $I)
$I->wantToTest('Mvc\Router - handle()');

$router = new Router();
$router->setDI(new FactoryDefault());

$router->add(
'/admin/invoices/list',
Expand Down Expand Up @@ -77,6 +78,7 @@ public function mvcRouterHandleWithPlaceholders(IntegrationTester $I)
* Regular placeholders
*/
$router = new Router(false);
$router->setDI(new FactoryDefault());
$router->add(
'/:module/:namespace/:controller/:action/:params/:int',
[
Expand Down Expand Up @@ -227,6 +229,7 @@ public function mvcRouterHandleShortSyntax(IntegrationTester $I)
$I->wantToTest('Mvc\Router - handle() - short syntax');

$router = new Router(false);
$router->setDI(new FactoryDefault());
$router->add("/about", "About::content");

$router->handle('/about');
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/Mvc/Router/Refactor/GroupCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Codeception\Example;
use IntegrationTester;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Router\Group;
use Phalcon\Mvc\Router\Route;
Expand All @@ -38,6 +39,7 @@ public function testGroups(IntegrationTester $I, Example $example)
Route::reset();

$router = new Router(false);
$router->setDI(new FactoryDefault());

$blog = new Group(
[
Expand Down Expand Up @@ -143,7 +145,6 @@ public function testHostnameRouteGroup(IntegrationTester $I, Example $example)
$container = $this->getDi();

$router = new Router(false);

$router->setDI($container);

$router->add(
Expand Down