Skip to content

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

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

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

Conversation

@iliaal

@iliaal iliaal commented Jul 29, 2026

Copy link
Copy Markdown
Owner

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
@iliaal
iliaal force-pushed the fix/json-hooked-prop-exception-leak branch from 56e50e4 to 9de3544 Compare July 29, 2026 12:49
@iliaal

iliaal commented Jul 29, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream as php#22919.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant