Skip to content

Fix leak of a hooked property value when the get hook throws in var_export() and get_object_vars() - #25830

Closed
lacatoire wants to merge 2 commits into
php:PHP-8.4from
lacatoire:fix/standard-var-export-hook-leak-PHP-8.4
Closed

lacatoire wants to merge 2 commits into
php:PHP-8.4from
lacatoire:fix/standard-var-export-hook-leak-PHP-8.4

Conversation

@lacatoire

@lacatoire lacatoire commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

var_export() and get_object_vars() read a hooked property into a local zval and bail out when an exception is pending, without releasing it. The get hook can fill that zval and still throw while its frame is freed, for example from the destructor of one of its locals. GH-22919 fixed the same leak in json_encode(); this applies the same fix to the two other readers.

To reproduce

class Thrower { function __destruct() { throw new Exception('destructor'); } }
class Hooked { public string $prop { get { $local = new Thrower; return str_repeat('y', 100000); } } }

foreach (['json_encode', 'var_export', 'get_object_vars'] as $function) {
    $export = fn() => $function === 'var_export' ? var_export(new Hooked, true) : $function(new Hooked);
    try { $export(); } catch (Exception $e) {}
    $before = memory_get_usage();
    for ($i = 0; $i < 100; $i++) {
        try { $export(); } catch (Exception $e) {}
    }
    printf("%s: %d bytes\n", $function, memory_get_usage() - $before);
}

Before (8.4.28, 8.5.13):

json_encode: 0 bytes
var_export: 10240000 bytes
get_object_vars: 10240000 bytes

After:

json_encode: 0 bytes
var_export: 0 bytes
get_object_vars: 0 bytes

The value read from a hooked property was not released when an
exception was pending after the read, as json_encode() already does.
The get hook can publish its return value into tmp and then throw while
its frame is freed, for example from the destructor of one of its
locals. Destroy it on the exception exit, as json_encode() does.
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.

1 participant