Skip to content

Commit fc6f53d

Browse files
committed
Fix leak when setting cyclic previous exception in finally
A curious exception handling pattern found in Symfony's HttpClient.
1 parent 7f3bc64 commit fc6f53d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Leak when setting recursive previous exception in finally handling
3+
--FILE--
4+
<?php
5+
6+
try {
7+
try {
8+
throw new Exception("Test");
9+
} catch (Exception $e) {
10+
throw $e;
11+
} finally {
12+
throw $e;
13+
}
14+
} catch (Exception $e2) {
15+
echo $e2->getMessage(), "\n";
16+
}
17+
18+
?>
19+
--EXPECT--
20+
Test

Zend/zend_exceptions.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ void zend_exception_set_previous(zend_object *exception, zend_object *add_previo
7676
zval pv, zv, rv;
7777
zend_class_entry *base_ce;
7878

79-
if (exception == add_previous || !add_previous || !exception) {
79+
if (!exception || !add_previous) {
8080
return;
8181
}
82+
83+
if (exception == add_previous) {
84+
OBJ_RELEASE(add_previous);
85+
return;
86+
}
87+
8288
ZVAL_OBJ(&pv, add_previous);
8389
if (!instanceof_function(Z_OBJCE(pv), zend_ce_throwable)) {
8490
zend_error_noreturn(E_CORE_ERROR, "Previous exception must implement Throwable");

0 commit comments

Comments
 (0)