Skip to content

Commit 6850a04

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix freeing of incompletely initialized closures
2 parents b4dc85f + af2110e commit 6850a04

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PHP NEWS
88
(Jeremie Courreges-Anglas)
99
. Fixed bug GH-11876: ini_parse_quantity() accepts invalid quantities.
1010
(Girgias)
11+
. Fixed bug GH-12073 (Segfault when freeing incompletely initialized
12+
closures). (ilutov)
1113

1214
- DOM:
1315
. Fix memory leak when setting an invalid DOMDocument encoding. (nielsdos)

Zend/tests/gh12073.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
GH-12073: Freeing of non-ZMM pointer of incompletely allocated closure
3+
--SKIPIF--
4+
<?php
5+
if (getenv("USE_ZEND_ALLOC") === "0" && getenv("USE_TRACKED_ALLOC") !== "1") {
6+
die("skip Zend MM disabled");
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
function test() {
13+
$k = 1;
14+
return function () use ($k) {
15+
foo();
16+
};
17+
}
18+
19+
ini_set('memory_limit', '2M');
20+
21+
$array = [];
22+
for ($i = 0; $i < 10_000; $i++) {
23+
$array[] = test();
24+
}
25+
26+
?>
27+
--EXPECTF--
28+
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d

Zend/zend_closures.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,11 @@ static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_en
725725
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
726726
closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE;
727727

728+
zend_string_addref(closure->func.op_array.function_name);
729+
if (closure->func.op_array.refcount) {
730+
(*closure->func.op_array.refcount)++;
731+
}
732+
728733
/* For fake closures, we want to reuse the static variables of the original function. */
729734
if (!is_fake) {
730735
if (closure->func.op_array.static_variables) {
@@ -758,22 +763,17 @@ static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_en
758763
if (func->common.scope != scope) {
759764
func->common.scope = scope;
760765
}
761-
closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE;
762766
ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
763767
ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr);
768+
closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE;
764769
} else {
765770
/* Otherwise, we use a non-shared runtime cache */
766-
closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
767771
ptr = emalloc(func->op_array.cache_size);
772+
closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
768773
}
769774
memset(ptr, 0, func->op_array.cache_size);
770775
}
771776
ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr);
772-
773-
zend_string_addref(closure->func.op_array.function_name);
774-
if (closure->func.op_array.refcount) {
775-
(*closure->func.op_array.refcount)++;
776-
}
777777
} else {
778778
memcpy(&closure->func, func, sizeof(zend_internal_function));
779779
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;

0 commit comments

Comments
 (0)