Skip to content

Commit 224f66e

Browse files
committed
Fix memory leak on case of failure
1 parent cd4678c commit 224f66e

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Zend/zend_API.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,11 +1850,14 @@ ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *c
18501850
{
18511851
zend_result status = _object_and_properties_init(arg, class_type, NULL);
18521852
if (UNEXPECTED(status == FAILURE)) {
1853+
ZVAL_UNDEF(arg);
18531854
return FAILURE;
18541855
}
18551856
zend_object *obj = Z_OBJ_P(arg);
18561857
zend_function *constructor = obj->handlers->get_constructor(obj);
18571858
if (UNEXPECTED(constructor == NULL)) {
1859+
zval_ptr_dtor(arg);
1860+
ZVAL_UNDEF(arg);
18581861
return FAILURE;
18591862
}
18601863
/* A constructor should not return a value, however if an exception is thrown
@@ -1870,6 +1873,8 @@ ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *c
18701873
named_params
18711874
);
18721875
if (Z_TYPE(retval) == IS_UNDEF) {
1876+
zval_ptr_dtor(arg);
1877+
ZVAL_UNDEF(arg);
18731878
return FAILURE;
18741879
} else {
18751880
/* Unlikely, but user constructors may return any value they want */

ext/zend_test/test.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,14 @@ static ZEND_FUNCTION(zend_object_init_with_constructor)
473473
Z_PARAM_VARIADIC_WITH_NAMED(args, num_args, named_args)
474474
ZEND_PARSE_PARAMETERS_END();
475475

476-
zend_result status = object_init_with_constructor(return_value, ce, num_args, args, named_args);
476+
zval obj;
477+
/* We don't use return_value directly to check for memory leaks of the API on failure */
478+
zend_result status = object_init_with_constructor(&obj, ce, num_args, args, named_args);
477479
if (status == FAILURE) {
478480
RETURN_THROWS();
479481
}
480482
ZEND_ASSERT(!EG(exception));
483+
ZVAL_COPY_VALUE(return_value, &obj);
481484
}
482485

483486
static ZEND_FUNCTION(zend_get_unit_enum)

0 commit comments

Comments
 (0)