8000 bug #25364 [DependencyInjection] Prevent a loop in aliases within the… · symfony/symfony@6e7e684 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e7e684

Browse files
committed
bug #25364 [DependencyInjection] Prevent a loop in aliases within the findDefinition method (sroze)
This PR was merged into the 3.3 branch. Discussion ---------- [DependencyInjection] Prevent a loop in aliases within the `findDefinition` method | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25338 | License | MIT | Doc PR | ø This prevents an infinite loop going when aliases reference themselves. This is based on 3.3 as the "normalized ID" changed to allow non-lowercase names. Fixing this in 2.7 would mean a merge conflict that IMO is not worth it. Commits ------- 22f3523 Prevent a loop in aliases within the `findDefinition` method
2 parents b783602 + 22f3523 commit 6e7e684

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,15 @@ public function findDefinition($id)
993993
{
994994
$id = $this->normalizeId($id);
995995

996+
$seen = array();
996997
while (isset($this->aliasDefinitions[$id])) {
997998
$id = (string) $this->aliasDefinitions[$id];
999+
1000+
if (isset($seen[$id])) {
1001+
throw new ServiceCircularReferenceException($id, array_keys($seen));
1002+
}
1003+
1004+
$seen[$id] = true;
9981005
}
9991006

10001007
return $this->getDefinition($id);

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,22 @@ public function testInlinedDefinitions()
10441044
$this->assertNotSame($bar->foo, $barUser->foo);
10451045
}
10461046

1047+
/**
1048+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
1049+
* @expectedExceptionMessage Circular reference detected for service "app.test_class", path: "app.test_class -> App\TestClass".
1050+
*/
1051+
public function testThrowsCircularExceptionForCircularAliases()
1052+
{
1053+
$builder = new ContainerBuilder();
1054+
1055+
$builder->setAliases(array(
1056+
'app.test_class' => new Alias('App\\TestClass'),
1057+
'App\\TestClass' => new Alias('app.test_class'),
1058+
));
1059+
1060+
$builder->findDefinition('App\\TestClass');
1061+
}
1062+
10471063
public function testInitializePropertiesBeforeMethodCalls()
10481064
{
10491065
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)
0