diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2020-06-30 12:22:41 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-06-30 12:24:32 +0200 |
commit | fc6f53d426bde3e3ab4e73d44abba54fdb9891f7 (patch) | |
tree | 1121036bee4e696b3eb29d76bb2131dce8a2dfc2 | |
parent | 7f3bc64287588f6a838d1a9524624deba1e5e153 (diff) | |
download | php-git-fc6f53d426bde3e3ab4e73d44abba54fdb9891f7.tar.gz |
Fix leak when setting cyclic previous exception in finally
A curious exception handling pattern found in Symfony's HttpClient.
-rw-r--r-- | Zend/tests/exception_set_previous_leak.phpt | 20 | ||||
-rw-r--r-- | Zend/zend_exceptions.c | 8 |
2 files changed, 27 insertions, 1 deletions
diff --git a/Zend/tests/exception_set_previous_leak.phpt b/Zend/tests/exception_set_previous_leak.phpt new file mode 100644 index 0000000000..7fb680f951 --- /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-- +<?php + +try { + try { + throw new Exception("Test"); + } catch (Exception $e) { + throw $e; + } finally { + throw $e; + } +} catch (Exception $e2) { + echo $e2->getMessage(), "\n"; +} + +?> +--EXPECT-- +Test diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c index 3ad5748e4d..371c38dcd6 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"); |