Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d223a5e

Browse files
bug #39738 [VarDumper] fix mutating $GLOBALS while cloning it (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [VarDumper] fix mutating $GLOBALS while cloning it | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #39679 | License | MIT | Doc PR | - Also preparing for https://wiki.php.net/rfc/restrict_globals_usage Commits ------- 384b0ad [VarDumper] fix mutating $GLOBALS while cloning it
2 parents a147705 + 384b0ad commit d223a5e

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

src/Symfony/Component/VarDumper/Cloner/Data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs,
335335
}
336336
$cursor->hardRefTo = $refs[$r];
337337
$cursor->hardRefHandle = $this->useRefHandles & $item->handle;
338-
$cursor->hardRefCount = $item->refCount;
338+
$cursor->hardRefCount = 0 < $item->handle ? $item->refCount : 0;
339339
}
340340
$cursor->attr = $item->attr;
341341
$type = $item->class ?: \gettype($item->value);

src/Symfony/Component/VarDumper/Cloner/VarCloner.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,19 @@ protected function doClone($var)
159159
if (Stub::ARRAY_ASSOC === $stub->class) {
160160
// Copies of $GLOBALS have very strange behavior,
161161
// let's detect them with some black magic
162-
$a[$gid] = true;
163-
164-
// Happens with copies of $GLOBALS
165-
if (isset($v[$gid])) {
162+
if (\PHP_VERSION_ID < 80100 && ($a[$gid] = true) && isset($v[$gid])) {
166163
unset($v[$gid]);
167164
$a = [];
168165
foreach ($v as $gk => &$gv) {
166+
if ($v === $gv) {
167+
unset($v);
168+
$v = new Stub();
169+
$v->value = [$v->cut = \count($gv), Stub::TYPE_ARRAY => 0];
170+
$v->handle = -1;
171+
$gv = &$hardRefs[spl_object_id($v)];
172+
$gv = $v;
173+
}
174+
169175
$a[$gk] = &$gv;
170176
}
171177
unset($gv);

src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ public function testRefsInProperties()
439439
/**
440440
* @runInSeparateProcess
441441
* @preserveGlobalState disabled
442+
* @requires PHP < 8.1
442443
*/
443444
public function testSpecialVars56()
444445
{
@@ -453,11 +454,11 @@ public function testSpecialVars56()
453454
]
454455
]
455456
1 => array:1 [
456-
"GLOBALS" => &2 array:1 [
457-
"GLOBALS" => &2 array:1 [&2]
458-
]
457+
"GLOBALS" => & array:1 [ …1]
458+
]
459+
2 => &3 array:1 [
460+
"GLOBALS" => &3 array:1 [&3]
459461
]
460-
2 => &2 array:1 [&2]
461462
]
462463
EOTXT
463464
,
@@ -468,6 +469,7 @@ public function testSpecialVars56()
468469
/**
469470
* @runInSeparateProcess
470471
* @preserveGlobalState disabled
472+
* @requires PHP < 8.1
471473
*/
472474
public function testGlobals()
473475
{
@@ -490,11 +492,11 @@ public function testGlobals()
490492
<<<'EOTXT'
491493
array:2 [
492494
1 => array:1 [
493-
"GLOBALS" => &1 array:1 [
494-
"GLOBALS" => &1 array:1 [&1]
495-
]
495+
"GLOBALS" => & array:1 [ …1]
496+
]
497+
2 => &2 array:1 [
498+
"GLOBALS" => &2 array:1 [&2]
496499
]
497-
2 => &1 array:1 [&1]
498500
]
499501

500502
EOTXT
@@ -584,6 +586,6 @@ private function getSpecialVars()
584586
return $var;
585587
};
586588

587-
return [$var(), $GLOBALS, &$GLOBALS];
589+
return eval('return [$var(), $GLOBALS, &$GLOBALS];');
588590
}
589591
}

0 commit comments

Comments
 (0)