Skip to content

Commit baf74ed

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix freeing of incompletely initialized closures
2 parents 6d0b27d + 6850a04 commit baf74ed

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
@@ -6,6 +6,8 @@ PHP NEWS
66
. Fixed GH-11847 (DTrace enabled build is broken). (Filip Zrůst)
77
. Fixed bug GH-11876: ini_parse_quantity() accepts invalid quantities.
88
(Girgias)
9+
. Fixed bug GH-12073 (Segfault when freeing incompletely initialized
10+
closures). (ilutov)
911

1012
- FPM:
1113
. Fixed GH-12077 (PHP 8.3.0RC1 borked socket-close-on-exec.phpt).

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
@@ -732,6 +732,11 @@ static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_en
732732
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;
733733
closure->func.common.fn_flags &= ~ZEND_ACC_IMMUTABLE;
734734

735+
zend_string_addref(closure->func.op_array.function_name);
736+
if (closure->func.op_array.refcount) {
737+
(*closure->func.op_array.refcount)++;
738+
}
739+
735740
/* For fake closures, we want to reuse the static variables of the original function. */
736741
if (!is_fake) {
737742
if (closure->func.op_array.static_variables) {
@@ -765,22 +770,17 @@ static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_en
765770
if (func->common.scope != scope) {
766771
func->common.scope = scope;
767772
}
768-
closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE;
769773
ptr = zend_arena_alloc(&CG(arena), func->op_array.cache_size);
770774
ZEND_MAP_PTR_SET(func->op_array.run_time_cache, ptr);
775+
closure->func.op_array.fn_flags &= ~ZEND_ACC_HEAP_RT_CACHE;
771776
} else {
772777
/* Otherwise, we use a non-shared runtime cache */
773-
closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
774778
ptr = emalloc(func->op_array.cache_size);
779+
closure->func.op_array.fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
775780
}
776781
memset(ptr, 0, func->op_array.cache_size);
777782
}
778783
ZEND_MAP_PTR_INIT(closure->func.op_array.run_time_cache, ptr);
779-
780-
zend_string_addref(closure->func.op_array.function_name);
781-
if (closure->func.op_array.refcount) {
782-
(*closure->func.op_array.refcount)++;
783-
}
784784
} else {
785785
memcpy(&closure->func, func, sizeof(zend_internal_function));
786786
closure->func.common.fn_flags |= ZEND_ACC_CLOSURE;

0 commit comments

Comments
 (0)