Skip to content

Fix #80663: Recursive SplFixedArray::setSize() may cause double-free #7503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions ext/spl/spl_fixedarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,20 @@ static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size) /* {{{

/* clearing the array */
if (size == 0) {
zend_long i;

for (i = 0; i < array->size; i++) {
zval_ptr_dtor(&(array->elements[i]));
}
if (array->elements != NULL) {
zend_long i;
zval *elements = array->elements;
zend_long old_size = array->size;

if (array->elements) {
efree(array->elements);
array->elements = NULL;
array->size = 0;

for (i = 0; i < old_size; i++) {
zval_ptr_dtor(&(elements[i]));
}

efree(elements);
return;
}
} else if (size > array->size) {
array->elements = safe_erealloc(array->elements, size, sizeof(zval), 0);
Expand Down
15 changes: 15 additions & 0 deletions ext/spl/tests/bug80663.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Bug #80663 (Recursive SplFixedArray::setSize() may cause double-free)
--FILE--
<?php
class InvalidDestructor {
public function __destruct() {
$GLOBALS['obj']->setSize(0);
}
}

$obj = new SplFixedArray(1000);
$obj[0] = new InvalidDestructor();
$obj->setSize(0);
?>
--EXPECT--