Skip to content

Free the hooked property value when json_encode() sees an exception - #22919

Closed
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/json-hooked-prop-exception-leak
Closed

Free the hooked property value when json_encode() sees an exception#22919
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/json-hooked-prop-exception-leak

Conversation

@iliaal

@iliaal iliaal commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

A get hook can publish its return value into the zval json_encode() handed to zend_read_property_ex(), then throw while the engine tears the hook's frame down, for instance from the destructor of one of its locals. php_json_encode_array() returns on EG(exception) without destroying that zval, so the value survives to request shutdown. Twenty encodes of a hook returning a 1 MiB string retain 21 MB before the patch, 1.5 KB after.

php_json_encode_array() reads a hooked property into a local zval and
bails out when an exception is pending. The get hook can publish its
return value into that zval and then throw while its frame is freed,
for example from the destructor of one of its locals, so the bailout
leaks the value until request shutdown. Destroy it on the way out, the
way the other failure exit already does.

Closes phpGH-22919

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an obvious inconsistency with the php_json_encode_zval() error handling further below.

@iliaal iliaal closed this in c35253d Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants