xref: /PHP-8.3/Zend/tests/object_handlers.phpt (revision 75a678a7)
1--TEST--
2Magic object handlers segfaults and memory errors
3--FILE--
4<?php
5function f($x) {
6    return $x;
7}
8
9class foo implements ArrayAccess {
10    function __get($property) {
11        $GLOBALS["y"] = $property;
12    }
13    function __set($property, $value) {
14        $GLOBALS["y"] = $property;
15    }
16    function __call($func, $args) {
17        $GLOBALS["y"] = $func;
18    }
19    static function __callStatic($func, $args) {
20        $GLOBALS["y"] = $func;
21    }
22    function offsetGet($index): mixed {
23        $GLOBALS["y"] = $index;
24        return null;
25    }
26    function offsetSet($index, $newval): void {
27        $GLOBALS["y"] = $index;
28    }
29    function offsetExists($index): bool {
30        $GLOBALS["y"] = $index;
31        return true;
32    }
33    function offsetUnset($index): void {
34        $GLOBALS["y"] = $index;
35    }
36}
37
38$x = new foo();
39$y = null;
40
41// IS_CONST
42$z = $x->const_get;
43echo $y,"\n";
44$x->const_set = 1;
45echo $y,"\n";
46$x->const_call();
47echo $y,"\n";
48foo::const_callstatic();
49echo $y,"\n";
50$z = $x["const_dim_get"];
51echo $y,"\n";
52$x["const_dim_set"] = 1;
53echo $y,"\n";
54isset($x["const_dim_isset"]);
55echo $y,"\n";
56unset($x["const_dim_unset"]);
57echo $y,"\n";
58
59// IS_CONST + conversion
60$z = $x->{1};
61echo $y,"\n";
62$x->{2} = 1;
63echo $y,"\n";
64
65// IS_TMP_VAR
66$c = "tmp";
67$z = $x->{$c."_get"};
68echo $y,"\n";
69$x->{$c."_set"} = 1;
70echo $y,"\n";
71$x->{$c."_call"}();
72echo $y,"\n";
73$z = $x[$c."_dim_get"];
74echo $y,"\n";
75$x[$c."_dim_set"] = 1;
76echo $y,"\n";
77isset($x[$c."_dim_isset"]);
78echo $y,"\n";
79unset($x[$c."_dim_unset"]);
80echo $y,"\n";
81
82// IS_TMP_VAR + conversion
83$c = 0;
84$z = $x->{$c+3};
85echo $y,"\n";
86$x->{$c+4} = 1;
87echo $y,"\n";
88
89// IS_CV
90$c = "cv_get";
91$z = $x->{$c};
92echo $y,"\n";
93$c = "cv_set";
94$x->{$c} = 1;
95echo $y,"\n";
96$c = "cv_call";
97$x->{$c}();
98echo $y,"\n";
99$c = "cv_dim_get";
100$z = $x[$c];
101echo $y,"\n";
102$c = "cv_dim_set";
103$x[$c] = 1;
104echo $y,"\n";
105$c = "cv_dim_isset";
106isset($x[$c]);
107echo $y,"\n";
108$c = "cv_dim_unset";
109unset($x[$c]);
110echo $y,"\n";
111
112// IS_CV + conversion
113$c = 5;
114$z = $x->{$c};
115echo $y,"\n";
116$c = 6;
117$x->{$c} = 1;
118echo $y,"\n";
119
120// IS_VAR
121$z = $x->{f("var_get")};
122echo $y,"\n";
123$x->{f("var_set")} = 1;
124echo $y,"\n";
125$x->{f("var_call")}();
126echo $y,"\n";
127$z = $x[f("var_dim_get")];
128echo $y,"\n";
129$x[f("var_dim_set")] = 1;
130echo $y,"\n";
131isset($x[f("var_dim_isset")]);
132echo $y,"\n";
133unset($x[f("var_dim_unset")]);
134echo $y,"\n";
135
136// IS_VAR + conversion
137$z = $x->{f(7)};
138echo $y,"\n";
139$x->{f(8)} = 1;
140echo $y,"\n";
141?>
142--EXPECT--
143const_get
144const_set
145const_call
146const_callstatic
147const_dim_get
148const_dim_set
149const_dim_isset
150const_dim_unset
1511
1522
153tmp_get
154tmp_set
155tmp_call
156tmp_dim_get
157tmp_dim_set
158tmp_dim_isset
159tmp_dim_unset
1603
1614
162cv_get
163cv_set
164cv_call
165cv_dim_get
166cv_dim_set
167cv_dim_isset
168cv_dim_unset
1695
1706
171var_get
172var_set
173var_call
174var_dim_get
175var_dim_set
176var_dim_isset
177var_dim_unset
1787
1798
180