1--TEST--
2Bug #78438 (Corruption when __unserializing deeply nested structures)
3--FILE--
4<?php
5
6class Node
7{
8    public $childs = [];
9
10    public function __serialize()
11    {
12        return [$this->childs];
13    }
14
15    public function __unserialize(array $data)
16    {
17        list($this->childs) = $data;
18    }
19}
20
21function createTree ($width, $depth) {
22    $root = new Node();
23
24    $nextLevel = [$root];
25
26    for ($level=1; $level<$depth; $level++) {
27        $levelRoots = $nextLevel;
28        $nextLevel = [];
29
30        while (count($levelRoots) > 0) {
31            $levelRoot = array_shift($levelRoots);
32
33            for ($w = 0; $w < $width; $w++) {
34                $tester = new Node();
35
36                $levelRoot->childs[] = $tester;
37
38                $nextLevel[] = $tester;
39            }
40        }
41    }
42
43    return $root;
44}
45
46
47$width = 3;
48ob_implicit_flush();
49
50foreach (range(1, 8) as $depth) {
51    $tree = createTree($width, $depth);
52
53    echo "Testcase tree $width x $depth".PHP_EOL;
54
55    echo "> Serializing now".PHP_EOL;
56    $serialized = serialize($tree);
57
58    echo "> Unserializing now".PHP_EOL;
59    $tree = unserialize($serialized);
60
61    // Lets test whether all is ok!
62    $expectedSize = ($width**$depth - 1)/($width-1);
63
64    $nodes = [$tree];
65    $count = 0;
66
67    while (count($nodes) > 0) {
68        $count++;
69
70        $node = array_shift($nodes);
71        foreach ($node->childs as $node) {
72            $nodes[] = $node;
73        }
74    }
75
76    echo "> Unserialized total node count was $count, expected $expectedSize: ".($expectedSize === $count ? 'CORRECT!' : 'INCORRECT');
77
78    echo PHP_EOL;
79    echo PHP_EOL;
80}
81?>
82--EXPECT--
83Testcase tree 3 x 1
84> Serializing now
85> Unserializing now
86> Unserialized total node count was 1, expected 1: CORRECT!
87
88Testcase tree 3 x 2
89> Serializing now
90> Unserializing now
91> Unserialized total node count was 4, expected 4: CORRECT!
92
93Testcase tree 3 x 3
94> Serializing now
95> Unserializing now
96> Unserialized total node count was 13, expected 13: CORRECT!
97
98Testcase tree 3 x 4
99> Serializing now
100> Unserializing now
101> Unserialized total node count was 40, expected 40: CORRECT!
102
103Testcase tree 3 x 5
104> Serializing now
105> Unserializing now
106> Unserialized total node count was 121, expected 121: CORRECT!
107
108Testcase tree 3 x 6
109> Serializing now
110> Unserializing now
111> Unserialized total node count was 364, expected 364: CORRECT!
112
113Testcase tree 3 x 7
114> Serializing now
115> Unserializing now
116> Unserialized total node count was 1093, expected 1093: CORRECT!
117
118Testcase tree 3 x 8
119> Serializing now
120> Unserializing now
121> Unserialized total node count was 3280, expected 3280: CORRECT!
122