Skip to content

Commit 8689593

Browse files
committed
Fix incorrect handling of hooked props without get hook in get_object_vars()
Fixes phpGH-17988 Closes phpGH-17997
1 parent 8950c24 commit 8689593

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PHP NEWS
1313
results for closures created from magic __call()). (timwolla)
1414
. Fixed bug GH-17941 (Stack-use-after-return with lazy objects and hooks).
1515
(nielsdos)
16+
. Fixed bug GH-17988 (Incorrect handling of hooked props without get hook in
17+
get_object_vars()). (ilutov)
1618

1719
- DOM:
1820
. Fixed bug GH-17991 (Assertion failure dom_attr_value_write). (nielsdos)
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
GH-17988: Incorrect handling of hooked properties without get hook in get_object_vars()
3+
--FILE--
4+
<?php
5+
6+
class C
7+
{
8+
public string $prop {
9+
set => $value;
10+
}
11+
}
12+
13+
$c = new C;
14+
$c->prop = 42;
15+
16+
var_dump($c);
17+
var_dump(get_object_vars($c));
18+
var_export($c);
19+
echo "\n";
20+
var_dump(json_encode($c));
21+
var_dump((array)$c);
22+
23+
?>
24+
--EXPECTF--
25+
object(C)#%d (1) {
26+
["prop"]=>
27+
string(2) "42"
28+
}
29+
array(1) {
30+
["prop"]=>
31+
string(2) "42"
32+
}
33+
\C::__set_state(array(
34+
'prop' => '42',
35+
))
36+
string(13) "{"prop":"42"}"
37+
array(1) {
38+
["prop"]=>
39+
string(2) "42"
40+
}

Zend/zend_builtin_functions.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -840,15 +840,14 @@ ZEND_FUNCTION(get_object_vars)
840840
}
841841
const char *unmangled_name_cstr = zend_get_unmangled_property_name(prop_info->name);
842842
zend_string *unmangled_name = zend_string_init(unmangled_name_cstr, strlen(unmangled_name_cstr), false);
843-
zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp);
843+
value = zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp);
844844
zend_string_release_ex(unmangled_name, false);
845845
if (EG(exception)) {
846846
zend_release_properties(properties);
847847
zval_ptr_dtor(return_value);
848848
ZVAL_UNDEF(return_value);
849849
RETURN_THROWS();
850850
}
851-
value = &tmp;
852851
}
853852
Z_TRY_ADDREF_P(value);
854853

ext/json/json_encoder.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,12 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
283283
if ((prop_info->flags & ZEND_ACC_VIRTUAL) && !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) {
284284
continue;
285285
}
286-
zend_read_property_ex(prop_info->ce, Z_OBJ_P(val), prop_info->name, /* silent */ true, &tmp);
286+
data = zend_read_property_ex(prop_info->ce, Z_OBJ_P(val), prop_info->name, /* silent */ true, &tmp);
287287
if (EG(exception)) {
288288
PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc);
289289
zend_release_properties(prop_ht);
290290
return FAILURE;
291291
}
292-
data = &tmp;
293292
}
294293

295294
if (need_comma) {

0 commit comments

Comments
 (0)