1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
--TEST--
Bug #78438 (Corruption when __unserializing deeply nested structures)
--FILE--
<?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, 8) 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;
}
?>
--EXPECT--
Testcase tree 3 x 1
> Serializing now
> Unserializing now
> Unserialized total node count was 1, expected 1: CORRECT!
Testcase tree 3 x 2
> Serializing now
> Unserializing now
> Unserialized total node count was 4, expected 4: CORRECT!
Testcase tree 3 x 3
> Serializing now
> Unserializing now
> Unserialized total node count was 13, expected 13: CORRECT!
Testcase tree 3 x 4
> Serializing now
> Unserializing now
> Unserialized total node count was 40, expected 40: CORRECT!
Testcase tree 3 x 5
> Serializing now
> Unserializing now
> Unserialized total node count was 121, expected 121: CORRECT!
Testcase tree 3 x 6
> Serializing now
> Unserializing now
> Unserialized total node count was 364, expected 364: CORRECT!
Testcase tree 3 x 7
> Serializing now
> Unserializing now
> Unserialized total node count was 1093, expected 1093: CORRECT!
Testcase tree 3 x 8
> Serializing now
> Unserializing now
> Unserialized total node count was 3280, expected 3280: CORRECT!
|