Skip to content

[VarDumper] fix mutating $GLOBALS while cloning it #39738

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 1 commit into from
Jan 7, 2021
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs,
}
$cursor->hardRefTo = $refs[$r];
$cursor->hardRefHandle = $this->useRefHandles & $item->handle;
$cursor->hardRefCount = $item->refCount;
$cursor->hardRefCount = 0 < $item->handle ? $item->refCount : 0;
}
$cursor->attr = $item->attr;
$type = $item->class ?: \gettype($item->value);
Expand Down
14 changes: 10 additions & 4 deletions src/Symfony/Component/VarDumper/Cloner/VarCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,19 @@ protected function doClone($var)
if (Stub::ARRAY_ASSOC === $stub->class) {
// Copies of $GLOBALS have very strange behavior,
// let's detect them with some black magic
$a[$gid] = true;

// Happens with copies of $GLOBALS
if (isset($v[$gid])) {
if (\PHP_VERSION_ID < 80100 && ($a[$gid] = true) && isset($v[$gid])) {
unset($v[$gid]);
$a = [];
foreach ($v as $gk => &$gv) {
if ($v === $gv) {
unset($v);
$v = new Stub();
$v->value = [$v->cut = \count($gv), Stub::TYPE_ARRAY => 0];
$v->handle = -1;
$gv = &$hardRefs[spl_object_id($v)];
$gv = $v;
}

$a[$gk] = &$gv;
}
unset($gv);
Expand Down
20 changes: 11 additions & 9 deletions src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ public function testRefsInProperties()
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @requires PHP < 8.1
*/
public function testSpecialVars56()
{
Expand All @@ -453,11 +454,11 @@ public function testSpecialVars56()
]
]
1 => array:1 [
"GLOBALS" => &2 array:1 [
"GLOBALS" => &2 array:1 [&2]
]
"GLOBALS" => & array:1 [ …1]
]
2 => &3 array:1 [
"GLOBALS" => &3 array:1 [&3]
]
2 => &2 array:1 [&2]
]
EOTXT
,
Expand All @@ -468,6 +469,7 @@ public function testSpecialVars56()
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @requires PHP < 8.1
*/
public function testGlobals()
{
Expand All @@ -490,11 +492,11 @@ public function testGlobals()
<<<'EOTXT'
array:2 [
1 => array:1 [
"GLOBALS" => &1 array:1 [
"GLOBALS" => &1 array:1 [&1]
]
"GLOBALS" => & array:1 [ …1]
]
2 => &2 array:1 [
"GLOBALS" => &2 array:1 [&2]
]
2 => &1 array:1 [&1]
]

EOTXT
Expand Down Expand Up @@ -584,6 +586,6 @@ private function getSpecialVars()
return $var;
};

return [$var(), $GLOBALS, &$GLOBALS];
return eval('return [$var(), $GLOBALS, &$GLOBALS];');
}
}