Skip to content

Fix zend_lazy_object_get_properties for object with prop ht, when init fails #15825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Zend/tests/lazy_objects/gh15823.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
GH-15823: Wrong expectations in zend_lazy_object_get_properties()
--FILE--
<?php

class C {
public int $a = 1;
}

$reflector = new ReflectionClass(C::class);
$calls = 0;
$obj = $reflector->newLazyGhost(function ($obj) use (&$calls) {
if ($calls++ === 0) {
throw new Error("initializer");
}
$obj->a = 2;
});

// Builds properties ht without lazy initialization
var_dump($obj);
try {
// Lazy initialization fails during fetching of properties ht
json_encode($obj);
} catch (Error $e) {
printf("%s: %s\n", $e::class, $e->getMessage());
}

var_dump(json_encode($obj));

?>
--EXPECTF--
lazy ghost object(C)#%d (0) {
["a"]=>
uninitialized(int)
}
Error: initializer
string(7) "{"a":2}"
6 changes: 4 additions & 2 deletions Zend/zend_lazy_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,10 @@ ZEND_API HashTable *zend_lazy_object_get_properties(zend_object *object)

zend_object *tmp = zend_lazy_object_init(object);
if (UNEXPECTED(!tmp)) {
ZEND_ASSERT(!object->properties || object->properties == &zend_empty_array);
return object->properties = (zend_array*) &zend_empty_array;
if (object->properties) {
return object->properties;
}
return object->properties = zend_new_array(0);
}

object = tmp;
Expand Down
Loading