Skip to content

Commit

Permalink
[VarExporter] Fix handling mangled property names returned by __sleep()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 16, 2023
1 parent 11401fe commit fdb022f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
8 changes: 3 additions & 5 deletions Internal/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,19 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
$n = substr($n, 1 + $i);
}
if (null !== $sleep) {
if (!isset($sleep[$n]) || ($i && $c !== $class)) {
if (!isset($sleep[$name]) && (!isset($sleep[$n]) || ($i && $c !== $class))) {
unset($arrayValue[$name]);
continue;
}
$sleep[$n] = false;
unset($sleep[$name], $sleep[$n]);
}
if (!\array_key_exists($name, $proto) || $proto[$name] !== $v || "\x00Error\x00trace" === $name || "\x00Exception\x00trace" === $name) {
$properties[$c][$n] = $v;
}
}
if ($sleep) {
foreach ($sleep as $n => $v) {
if (false !== $v) {
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), \E_USER_NOTICE);
}
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), \E_USER_NOTICE);
}
}
if (method_exists($class, '__unserialize')) {
Expand Down
8 changes: 8 additions & 0 deletions Tests/Fixtures/var-on-sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
'night',
],
],
'Symfony\\Component\\VarExporter\\Tests\\GoodNight' => [
'foo' => [
'afternoon',
],
'bar' => [
'morning',
],
],
],
$o[0],
[]
Expand Down
6 changes: 5 additions & 1 deletion Tests/VarExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,21 @@ public function setFlags($flags): void
class GoodNight
{
public $good;
protected $foo;
private $bar;

public function __construct()
{
unset($this->good);
$this->foo = 'afternoon';
$this->bar = 'morning';
}

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

return ['good'];
return ['good', 'foo', "\0*\0foo", "\0".__CLASS__."\0bar"];
}
}

Expand Down
2 changes: 1 addition & 1 deletion VarExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static function export($value, bool &$isStaticValue = null, array &$found
ksort($states);

$wakeups = [null];
foreach ($states as $k => $v) {
foreach ($states as $v) {
if (\is_array($v)) {
$wakeups[-$v[0]] = $v[1];
} else {
Expand Down

0 comments on commit fdb022f

Please sign in to comment.