Skip to content

[Cache] Fix possible infinite loop in CachePoolPass #53593

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ public function process(ContainerBuilder $container)
}

$notAliasedCacheClearerId = $this->cacheClearerId;
while ($container->hasAlias($this->cacheClearerId)) {
$this->cacheClearerId = (string) $container->getAlias($this->cacheClearerId);
while ($container->hasAlias($notAliasedCacheClearerId)) {
$notAliasedCacheClearerId = (string) $container->getAlias($notAliasedCacheClearerId);
Copy link
Contributor Author

@HypeMC HypeMC Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $clearers array should only use non-alias names for keys since $container->getDefinition() (on line 193) doesn't work with aliases.

}
if ($container->hasDefinition($this->cacheClearerId)) {
if ($container->hasDefinition($notAliasedCacheClearerId)) {
$clearers[$notAliasedCacheClearerId] = $allPools;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;

class CachePoolPassTest extends TestCase
{
Expand Down Expand Up @@ -232,4 +234,33 @@ public function testChainAdapterPool()
$this->assertInstanceOf(ChildDefinition::class, $doctrineCachePool);
$this->assertSame('cache.app', $doctrineCachePool->getParent());
}

public function testGlobalClearerAlias()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.container_class', 'app');
$container->setParameter('kernel.project_dir', 'foo');

$container->register('cache.default_clearer', Psr6CacheClearer::class);

$container->setDefinition('cache.system_clearer', new ChildDefinition('cache.default_clearer'));

$container->setDefinition('cache.foo_bar_clearer', new ChildDefinition('cache.default_clearer'));
$container->setAlias('cache.global_clearer', 'cache.foo_bar_clearer');

$container->register('cache.adapter.array', ArrayAdapter::class)
->setAbstract(true)
->addTag('cache.pool');

$cachePool = new ChildDefinition('cache.adapter.array');
$cachePool->addTag('cache.pool', ['clearer' => 'cache.system_clearer']);
$container->setDefinition('app.cache_pool', $cachePool);

$this->cachePoolPass->process($container);

$definition = $container->getDefinition('cache.foo_bar_clearer');

$this->assertTrue($definition->hasTag('cache.pool.clearer'));
$this->assertEquals(['app.cache_pool' => new Reference('app.cache_pool', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)], $definition->getArgument(0));
}
}