Skip to content

A closure that mutates its by-value captured array (use ($resolving)) and recurses into the method that created it traps (symfony ParameterBag::resolveString) #208

Closed

Description

@blacktrs

Summary

A closure that mutates its by-value captured array (use ($resolving)) and recurses into the method that created it traps (symfony ParameterBag::resolveString).

Repro

<?php
final class Bag
{
    /** @var array<string, mixed> */
    private array $p = ['a' => 'A-%b%', 'b' => 'B-%c%', 'c' => 'C', 'n' => 5, 'loop' => '%loop%'];

    public function get(string $k): mixed
    {
        if (!array_key_exists($k, $this->p)) { throw new RuntimeException("missing $k"); }
        return $this->p[$k];
    }

    public function resolveString(string $value, array $resolving = []): mixed
    {
        if (preg_match('/^%([^%\s]+)%$/', $value, $match)) {
            $key = $match[1];
            if (isset($resolving[$key])) { throw new LogicException('circular ' . implode(',', array_keys($resolving))); }
            $resolving[$key] = true;
            return $this->resolveString((string) $this->get($key), $resolving);
        }

        return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($resolving, $value) {
            if (!isset($match[1])) { return '%%'; }
            $key = $match[1];
            if (isset($resolving[$key])) { throw new LogicException('circular ' . implode(',', array_keys($resolving))); }
            $resolved = $this->get($key);
            if (!is_string($resolved) && !is_numeric($resolved)) { throw new RuntimeException('bad ' . $key); }
            $resolved = (string) $resolved;
            $resolving[$key] = true;

            return $this->resolveString($resolved, $resolving);
        }, $value);
    }
}

$b = new Bag();
foreach (['x %a% y', '%a%', 'p %a% q %c% r %n% s %%', 'plain', 'x %loop% y'] as $v) {
    try { echo $v, ' => ', $b->resolveString($v), "\n"; }
    catch (Throwable $e) { echo $v, ' => ', get_class($e), ': ', $e->getMessage(), "\n"; }
}
for ($i = 0; $i < 2000; $i++) { $b->resolveString('p %a% q %c% r %n% s %%'); }
echo "done\n";

Expected (php)

x %a% y => x A-B-C y
%a% => A-B-C
p %a% q %c% r %n% s %% => p A-B-C q C r 5 s %%
plain => plain
x %loop% y => x %loop% y => LogicException: circular loop
done

Actual (manticore 9bb9cd00, Darwin arm64)


[exit code 133]

Tracking

Known-bug repro: tests/aot/repro/crash/closure_mutates_captured_array_param_recursion.php — bash tests/aot/xfail.sh -k closure_mutates_captured_array_param_recursion reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Activity

  1. added
    bug
    crash
    on Oct 12, 2026
  2. added a commit that references this issue on Oct 14, 2026
    b196626
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcrash

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions