From acbde502bf4d3548ae9a1a8a9a3b6d47b668850d Mon Sep 17 00:00:00 2001 From: Karoly Negyesi Date: Sat, 25 Oct 2014 06:19:21 -0700 Subject: [PATCH] Break infinite loop while resolving aliases --- .../Compiler/ResolveReferencesToAliasesPass.php | 6 ++++++ .../Compiler/ResolveReferencesToAliasesPassTest.php | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php index 015bdebc8b..c90d76f48a 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php @@ -12,6 +12,7 @@ namespace Symfony\Component\DependencyInjection\Compiler; use Symfony\Component\DependencyInjection\Alias; +use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -84,7 +85,12 @@ class ResolveReferencesToAliasesPass implements CompilerPassInterface */ private function getDefinitionId($id) { + $seen = array(); while ($this->container->hasAlias($id)) { + if (isset($seen[$id])) { + throw new ServiceCircularReferenceException($id, array_keys($seen)); + } + $seen[$id] = true; $id = (string) $this->container->getAlias($id); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveReferencesToAliasesPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveReferencesToAliasesPassTest.php index ca089a556b..6fdc233941 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveReferencesToAliasesPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveReferencesToAliasesPassTest.php @@ -48,6 +48,17 @@ class ResolveReferencesToAliasesPassTest extends \PHPUnit_Framework_TestCase $this->assertEquals('foo', (string) $arguments[0]); } + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException + */ + public function testAliasCircularReference() + { + $container = new ContainerBuilder(); + $container->setAlias('bar', 'foo'); + $container->setAlias('foo', 'bar'); + $this->process($container); + } + protected function process(ContainerBuilder $container) { $pass = new ResolveReferencesToAliasesPass();