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); 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;