Skip to content

Commit cc065ba

Browse files
authored
Fix zend_lazy_object_get_properties for object with prop ht, when init fails (#15825)
zend_lazy_object_get_properties() is used by zend_std_get_properties_ex() to fetch the properties of lazy objects. It initializes the object and returns its properties. When initialization fails we return an empty ht because most callers do not check for NULL. We rely on the exception thrown during initialization. We also assign that empty ht to zend_object.properties for the same reasons. We asserted that zend_object.properties was either NULL or &zend_empty_array, but there are other cases in which a uninitialized lazy object may have a properties ht. Here I remove the assertion, and return the existing properties ht if there is one. Otherwise I return zend_new_array(0) instead of &zend_emtpy_array as not all callers expect an immutable array (e.g. FE_FETCH does not).
1 parent 5c1b945 commit cc065ba

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Zend/tests/lazy_objects/gh15823.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
GH-15823: Wrong expectations in zend_lazy_object_get_properties()
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
public int $a = 1;
8+
}
9+
10+
$reflector = new ReflectionClass(C::class);
11+
$calls = 0;
12+
$obj = $reflector->newLazyGhost(function ($obj) use (&$calls) {
13+
if ($calls++ === 0) {
14+
throw new Error("initializer");
15+
}
16+
$obj->a = 2;
17+
});
18+
19+
// Builds properties ht without lazy initialization
20+
var_dump($obj);
21+
try {
22+
// Lazy initialization fails during fetching of properties ht
23+
json_encode($obj);
24+
} catch (Error $e) {
25+
printf("%s: %s\n", $e::class, $e->getMessage());
26+
}
27+
28+
var_dump(json_encode($obj));
29+
30+
?>
31+
--EXPECTF--
32+
lazy ghost object(C)#%d (0) {
33+
["a"]=>
34+
uninitialized(int)
35+
}
36+
Error: initializer
37+
string(7) "{"a":2}"

Zend/zend_lazy_objects.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,10 @@ ZEND_API HashTable *zend_lazy_object_get_properties(zend_object *object)
624624

625625
zend_object *tmp = zend_lazy_object_init(object);
626626
if (UNEXPECTED(!tmp)) {
627-
ZEND_ASSERT(!object->properties || object->properties == &zend_empty_array);
628-
return object->properties = (zend_array*) &zend_empty_array;
627+
if (object->properties) {
628+
return object->properties;
629+
}
630+
return object->properties = zend_new_array(0);
629631
}
630632

631633
object = tmp;

0 commit comments

Comments
 (0)