Skip to content

Commit 9800845

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Fix #80663: Recursive SplFixedArray::setSize() may cause double-free
2 parents db16a3a + e73cc7a commit 9800845

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

ext/spl/spl_fixedarray.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,14 @@ static void spl_fixedarray_dtor_range(spl_fixedarray *array, zend_long from, zen
156156
*/
157157
static void spl_fixedarray_dtor(spl_fixedarray *array)
158158
{
159-
zend_long size = array->size;
160159
if (!spl_fixedarray_empty(array)) {
161-
spl_fixedarray_dtor_range(array, 0, size);
162-
efree(array->elements);
160+
zval *begin = array->elements, *end = array->elements + array->size;
161+
array->elements = NULL;
162+
array->size = 0;
163+
while (begin != end) {
164+
zval_ptr_dtor(--end);
165+
}
166+
efree(begin);
163167
}
164168
}
165169

ext/spl/tests/bug80663.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Bug #80663 (Recursive SplFixedArray::setSize() may cause double-free)
3+
--FILE--
4+
<?php
5+
class InvalidDestructor {
6+
public function __destruct() {
7+
$GLOBALS['obj']->setSize(0);
8+
}
9+
}
10+
11+
$obj = new SplFixedArray(1000);
12+
$obj[0] = new InvalidDestructor();
13+
$obj->setSize(0);
14+
?>
15+
--EXPECT--

0 commit comments

Comments
 (0)