Skip to content

Commit 51e52e2

Browse files
committed
Support for nested exceptions and fatal errors in destructors
1 parent 47edd8a commit 51e52e2

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Zend/zend_objects.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ ZEND_API void zend_objects_destroy_object(zend_object *object, zend_object_handl
103103
zval *file = zend_read_property(default_exception_ce, old_exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
104104
zval *line = zend_read_property(default_exception_ce, old_exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
105105

106+
zval_ptr_dtor(&EG(exception));
107+
EG(exception) = old_exception;
106108
zend_error(E_ERROR, "Ignoring exception from %s::__destruct() while an exception is already active (Uncaught %s in %s on line %ld)",
107109
object->ce->name, Z_OBJCE_P(old_exception)->name, Z_STRVAL_P(file), Z_LVAL_P(line));
108-
zval_ptr_dtor(&EG(exception));
109110
}
110111
EG(exception) = old_exception;
111112
}

Zend/zend_objects_API.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ ZEND_API void zend_objects_store_del_ref(zval *zobject TSRMLS_DC)
165165
ZEND_API void zend_objects_store_del_ref_by_handle(zend_object_handle handle TSRMLS_DC)
166166
{
167167
struct _store_object *obj;
168+
int failure = 0;
168169

169170
if (!EG(objects_store).object_buckets) {
170171
return;
@@ -182,12 +183,20 @@ ZEND_API void zend_objects_store_del_ref_by_handle(zend_object_handle handle TSR
182183
EG(objects_store).object_buckets[handle].destructor_called = 1;
183184

184185
if (obj->dtor) {
185-
obj->dtor(obj->object, handle TSRMLS_CC);
186+
zend_try {
187+
obj->dtor(obj->object, handle TSRMLS_CC);
188+
} zend_catch {
189+
failure = 1;
190+
} zend_end_try();
186191
}
187192
}
188193
if (obj->refcount == 1) {
189194
if (obj->free_storage) {
190-
obj->free_storage(obj->object TSRMLS_CC);
195+
zend_try {
196+
obj->free_storage(obj->object TSRMLS_CC);
197+
} zend_catch {
198+
failure = 1;
199+
} zend_end_try();
191200
}
192201
ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST();
193202
}
@@ -203,6 +212,9 @@ ZEND_API void zend_objects_store_del_ref_by_handle(zend_object_handle handle TSR
203212
fprintf(stderr, "Decreased refcount of object id #%d\n", handle);
204213
}
205214
#endif
215+
if (failure) {
216+
zend_bailout();
217+
}
206218
}
207219

208220
ZEND_API zend_object_value zend_objects_store_clone_obj(zval *zobject TSRMLS_DC)

0 commit comments

Comments
 (0)