Created
August 21, 2019 15:18
-
-
Save Devristo/878f4dbd9e30ee3a3c235f0d2ef3431d to your computer and use it in GitHub Desktop.
php7.4-serializer-bug-reproducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Node | |
{ | |
public $childs = []; | |
public function __serialize() | |
{ | |
return [$this->childs]; | |
} | |
public function __unserialize(array $data) | |
{ | |
list($this->childs) = $data; | |
} | |
} | |
function createTree ($width, $depth) { | |
$root = new Node(); | |
$nextLevel = [$root]; | |
for ($level=1; $level<$depth; $level++) { | |
$levelRoots = $nextLevel; | |
$nextLevel = []; | |
while (count($levelRoots) > 0) { | |
$levelRoot = array_shift($levelRoots); | |
for ($w = 0; $w < $width; $w++) { | |
$tester = new Node(); | |
$levelRoot->childs[] = $tester; | |
$nextLevel[] = $tester; | |
} | |
} | |
} | |
return $root; | |
} | |
$width = 3; | |
ob_implicit_flush(); | |
foreach (range(1, 100) as $depth) { | |
$tree = createTree($width, $depth); | |
echo "Testcase tree $width x $depth".PHP_EOL; | |
echo "> Serializing now".PHP_EOL; | |
$serialized = serialize($tree); | |
echo "> Unserializing now".PHP_EOL; | |
$tree = unserialize($serialized); | |
// Lets test whether all is ok! | |
$expectedSize = ($width**$depth - 1)/($width-1); | |
$nodes = [$tree]; | |
$count = 0; | |
while (count($nodes) > 0) { | |
$count++; | |
$node = array_shift($nodes); | |
foreach ($node->childs as $node) { | |
$nodes[] = $node; | |
} | |
} | |
echo "> Unserialized total node count was $count, expected $expectedSize: ".($expectedSize === $count ? 'CORRECT!' : 'INCORRECT'); | |
echo PHP_EOL; | |
echo PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small reproducer for bug report https://bugs.php.net/bug.php?id=78438 and the issue encountered in symfony/symfony#33214.
Sample output on PHP 7.4 beta 4, see for yourself at https://3v4l.org/C9dJE .