From 03dfb7f26f9da5d2d41e890dbc02df96f3fe6539 Mon Sep 17 00:00:00 2001 From: lacatoire Date: Thu, 26 Nov 2026 17:58:05 +0100 Subject: [PATCH 1/2] Fix var_export() leak when a get hook raises an exception The value read from a hooked property was not released when an exception was pending after the read, as json_encode() already does. --- .../var_export_hook_exception_leak.phpt | 25 +++++++++++++++++++ ext/standard/var.c | 3 +++ 2 files changed, 28 insertions(+) create mode 100644 ext/standard/tests/general_functions/var_export_hook_exception_leak.phpt diff --git a/ext/standard/tests/general_functions/var_export_hook_exception_leak.phpt b/ext/standard/tests/general_functions/var_export_hook_exception_leak.phpt new file mode 100644 index 000000000000..a91e5bff874b --- /dev/null +++ b/ext/standard/tests/general_functions/var_export_hook_exception_leak.phpt @@ -0,0 +1,25 @@ +--TEST-- +var_export() releases the value of a get hook that raises an exception +--FILE-- +getMessage(), "\n"; +} +?> +--EXPECT-- +destructor diff --git a/ext/standard/var.c b/ext/standard/var.c index dc398c5a598b..690ba795d953 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -679,6 +679,9 @@ PHPAPI zend_result php_var_export_ex(zval *struc, int level, smart_str *buf) /* val = zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp); zend_string_release_ex(unmangled_name, false); if (EG(exception)) { + if (val == &tmp) { + zval_ptr_dtor(&tmp); + } ZEND_GUARD_OR_GC_UNPROTECT_RECURSION(guard, EXPORT, zobj); zend_release_properties(myht); return FAILURE; From 07ce0813d144f514411eab05dc39af0d02ed1585 Mon Sep 17 00:00:00 2001 From: lacatoire Date: Fri, 27 Nov 2026 11:13:23 +0100 Subject: [PATCH 2/2] Fix get_object_vars() leak when a get hook raises an exception 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. --- .../get_object_vars_hook_exception_leak.phpt | 25 +++++++++++++++++++ Zend/zend_builtin_functions.c | 1 + 2 files changed, 26 insertions(+) create mode 100644 Zend/tests/property_hooks/get_object_vars_hook_exception_leak.phpt diff --git a/Zend/tests/property_hooks/get_object_vars_hook_exception_leak.phpt b/Zend/tests/property_hooks/get_object_vars_hook_exception_leak.phpt new file mode 100644 index 000000000000..a11321daf62b --- /dev/null +++ b/Zend/tests/property_hooks/get_object_vars_hook_exception_leak.phpt @@ -0,0 +1,25 @@ +--TEST-- +get_object_vars() releases the value of a get hook that raises an exception +--FILE-- +getMessage(), "\n"; +} +?> +--EXPECT-- +destructor diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index 7917f4c98a5a..d3b712043036 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -843,6 +843,7 @@ ZEND_FUNCTION(get_object_vars) value = zend_read_property_ex(prop_info->ce, zobj, unmangled_name, /* silent */ true, &tmp); zend_string_release_ex(unmangled_name, false); if (EG(exception)) { + zval_ptr_dtor(&tmp); zend_release_properties(properties); zval_ptr_dtor(return_value); ZVAL_UNDEF(return_value);