Skip to content

Commit

Permalink
Fix infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
arai-ta committed Feb 1, 2024
1 parent 9a04820 commit d2f5199
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
};
use League\Container\ContainerAwareTrait;
use League\Container\Exception\ContainerException;
use League\Container\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
use ReflectionClass;

Expand All @@ -20,6 +21,8 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface
use ArgumentResolverTrait;
use ContainerAwareTrait;

private const RECURSIVE_RESOLVE_MAX = 10;

/**
* @var string
*/
Expand Down Expand Up @@ -55,6 +58,11 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface
*/
protected $resolved;

/**
* @var int
*/
protected $recursiveResolveCount = 0;

/**
* @param string $id
* @param mixed|null $concrete
Expand Down Expand Up @@ -188,6 +196,12 @@ public function resolveNew()
// if we still have a string, try to pull it from the container
// this allows for `alias -> alias -> ... -> concrete
if (is_string($concrete) && $container instanceof ContainerInterface && $container->has($concrete)) {
if ($this->recursiveResolveCount++ > self::RECURSIVE_RESOLVE_MAX) {
throw new NotFoundException(
sprintf('Alias "%s" did not found or alias too deep.', $concrete)
);
}

$concrete = $container->get($concrete);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,6 @@ public function testNonExistentClassCausesException(): void
self::assertTrue($container->has(NonExistent::class));

$this->expectException(NotFoundException::class);
$container->get(NonExistent::class); // causes Fatal error: Allowed memory size of 134217728 bytes exhausted
$container->get(NonExistent::class);
}
}

0 comments on commit d2f5199

Please sign in to comment.