Skip to content

Commit 5cc8db8

Browse files
committed
Fix zend_lazy_object_get_properties for object with prop ht, when init fails
1 parent 090b53b commit 5cc8db8

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)