diff --git a/Zend/tests/exception_set_previous_leak.phpt b/Zend/tests/exception_set_previous_leak.phpt new file mode 100644 index 00000000000..7fb680f9516 --- /dev/null +++ b/Zend/tests/exception_set_previous_leak.phpt @@ -0,0 +1,20 @@ +--TEST-- +Leak when setting recursive previous exception in finally handling +--FILE-- +getMessage(), "\n"; +} + +?> +--EXPECT-- +Test diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c index 937bf842922..2d52e4e49e0 100644 --- a/Zend/zend_exceptions.c +++ b/Zend/zend_exceptions.c @@ -76,9 +76,15 @@ void zend_exception_set_previous(zend_object *exception, zend_object *add_previo zval pv, zv, rv; zend_class_entry *base_ce; - if (exception == add_previous || !add_previous || !exception) { + if (!exception || !add_previous) { return; } + + if (exception == add_previous) { + OBJ_RELEASE(add_previous); + return; + } + ZVAL_OBJ(&pv, add_previous); if (!instanceof_function(Z_OBJCE(pv), zend_ce_throwable)) { zend_error_noreturn(E_CORE_ERROR, "Previous exception must implement Throwable");