Skip to content

Commit a85ffa9

Browse files
bug #52618 [VarExporter] Fix handling mangled property names returned by __sleep() (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [VarExporter] Fix handling mangled property names returned by __sleep() | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #52614 | License | MIT Did you know that __sleep can return mangled property names? I didn't :) https://3v4l.org/3ckU7 Fixes doctrine/orm#11063 also. Commits ------- c5bd676 [VarExporter] Fix handling mangled property names returned by __sleep()
2 parents 4d140c3 + c5bd676 commit a85ffa9

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/Symfony/Component/VarExporter/Internal/Exporter.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,19 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
157157
$n = substr($n, 1 + $i);
158158
}
159159
if (null !== $sleep) {
160-
if (!isset($sleep[$n]) || ($i && $c !== $class)) {
160+
if (!isset($sleep[$name]) && (!isset($sleep[$n]) || ($i && $c !== $class))) {
161161
unset($arrayValue[$name]);
162162
continue;
163163
}
164-
$sleep[$n] = false;
164+
unset($sleep[$name], $sleep[$n]);
165165
}
166166
if (!\array_key_exists($name, $proto) || $proto[$name] !== $v || "\x00Error\x00trace" === $name || "\x00Exception\x00trace" === $name) {
167167
$properties[$c][$n] = $v;
168168
}
169169
}
170170
if ($sleep) {
171171
foreach ($sleep as $n => $v) {
172-
if (false !== $v) {
173-
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), \E_USER_NOTICE);
174-
}
172+
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), \E_USER_NOTICE);
175173
}
176174
}
177175
if (method_exists($class, '__unserialize')) {

src/Symfony/Component/VarExporter/Tests/Fixtures/var-on-sleep.php

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
'night',
1212
],
1313
],
14+
'Symfony\\Component\\VarExporter\\Tests\\GoodNight' => [
15+
'foo' => [
16+
'afternoon',
17+
],
18+
'bar' => [
19+
'morning',
20+
],
21+
],
1422
],
1523
$o[0],
1624
[]

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,21 @@ public function setFlags($flags): void
349349
class GoodNight
350350
{
351351
public $good;
352+
protected $foo;
353+
private $bar;
352354

353355
public function __construct()
354356
{
355357
unset($this->good);
358+
$this->foo = 'afternoon';
359+
$this->bar = 'morning';
356360
}
357361

358362
public function __sleep(): array
359363
{
360364
$this->good = 'night';
361365

362-
return ['good'];
366+
return ['good', 'foo', "\0*\0foo", "\0".__CLASS__."\0bar"];
363367
}
364368
}
365369

src/Symfony/Component/VarExporter/VarExporter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function export($value, bool &$isStaticValue = null, array &$found
8383
ksort($states);
8484

8585
$wakeups = [null];
86-
foreach ($states as $k => $v) {
86+
foreach ($states as $v) {
8787
if (\is_array($v)) {
8888
$wakeups[-$v[0]] = $v[1];
8989
} else {

0 commit comments

Comments
 (0)