xref: /PHP-8.2/ext/spl/tests/bug74669.phpt (revision adb45a63)
1--TEST--
2Bug #74669: Unserialize ArrayIterator broken
3--FILE--
4<?php
5
6class Container implements Iterator
7{
8    public $container;
9    public $iterator;
10
11    public function __construct()
12    {
13        $this->container = new ArrayObject();
14        $this->iterator = $this->container->getIterator();
15    }
16
17    public function append($element)
18    {
19        $this->container->append($element);
20    }
21
22    public function current(): mixed
23    {
24        return $this->iterator->current();
25    }
26
27    public function next(): void
28    {
29        $this->iterator->next();
30    }
31
32    public function key(): mixed
33    {
34        return $this->iterator->key();
35    }
36
37    public function valid(): bool
38    {
39        return $this->iterator->valid();
40    }
41
42    public function rewind(): void
43    {
44        $this->iterator->rewind();
45    }
46}
47
48class SelfArray extends ArrayObject
49{
50    public function __construct()
51    {
52        parent::__construct($this);
53    }
54}
55
56$container = new Container();
57$container->append('test1');
58$container->append('test2');
59$container->valid();
60$serialized = serialize($container);
61unset($container);
62
63$container = unserialize($serialized);
64
65foreach ($container as $key => $value) {
66    echo $key . ' => ' . $value . PHP_EOL;
67}
68
69$arObj = new ArrayObject(['test1', 'test2']);
70$serialized = serialize($container);
71unset($arObj);
72
73$arObj = unserialize($serialized);
74foreach($arObj as $key => $value) {
75    echo $key . ' => ' . $value . PHP_EOL;
76}
77
78$payload = 'x:i:33554432;O:8:"stdClass":0:{};m:a:0:{}';
79$str = 'C:11:"ArrayObject":' . strlen($payload) . ':{' . $payload . '}';
80
81$ao = unserialize($str);
82var_dump($ao['foo']);
83
84$selfArray = new SelfArray();
85$selfArray['foo'] = 'bar';
86var_dump($selfArray);
87$serialized = serialize($selfArray);
88var_dump($serialized);
89unset($selfArray);
90$selfArray = unserialize($serialized);
91var_dump($selfArray);
92var_dump($selfArray['foo']);
93
94?>
95--EXPECTF--
960 => test1
971 => test2
980 => test1
991 => test2
100
101Warning: Undefined array key "foo" in %s on line %d
102NULL
103object(SelfArray)#9 (1) {
104  ["foo"]=>
105  string(3) "bar"
106}
107string(77) "O:9:"SelfArray":4:{i:0;i:16777216;i:1;N;i:2;a:1:{s:3:"foo";s:3:"bar";}i:3;N;}"
108
109Deprecated: Creation of dynamic property SelfArray::$foo is deprecated in %s on line %d
110object(SelfArray)#9 (1) {
111  ["foo"]=>
112  string(3) "bar"
113}
114string(3) "bar"
115