Skip to content

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

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 1 commit 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
11 changes: 11 additions & 0 deletions ext/spl/spl_fixedarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ ZEND_GET_MODULE(spl_fixedarray)
typedef struct _spl_fixedarray { /* {{{ */
zend_long size;
zval *elements;
unsigned int is_resizing:1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared to creating a temporary copy of zvals to destroy, this would also add another 4 or 8 bytes (with 64-bit struct field alignment?) to each SplFixedArray instance (and a tiny bit more time initializing it), when most applications wouldn't use setSize (according to other remarks it was meant more as an escape hatch than as a way to support vector-like functionality)

Some progress was made in reducing SplFixedArray's memory usage and instantiation time in #6552

unsigned int reserved:31;
} spl_fixedarray;
/* }}} */

Expand Down Expand Up @@ -88,6 +90,7 @@ static void spl_fixedarray_init(spl_fixedarray *array, zend_long size) /* {{{ */
array->elements = NULL;
array->size = 0;
}
array->is_resizing = 0;
}
/* }}} */

Expand All @@ -104,6 +107,8 @@ static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size) /* {{{
return;
}

array->is_resizing = 1;

/* clearing the array */
if (size == 0) {
zend_long i;
Expand All @@ -129,6 +134,7 @@ static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size) /* {{{
}

array->size = size;
array->is_resizing = 0;
}
/* }}} */

Expand Down Expand Up @@ -738,6 +744,11 @@ SPL_METHOD(SplFixedArray, setSize)

intern = Z_SPLFIXEDARRAY_P(object);

if (intern->array.is_resizing) {
zend_throw_exception_ex(spl_ce_LogicException, 0, "recursive resize is not allowed");
return;
}

spl_fixedarray_resize(&intern->array, size);
RETURN_TRUE;
}
Expand Down
20 changes: 20 additions & 0 deletions ext/spl/tests/bug80663.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Bug #80663 (Recursive SplFixedArray::setSize() may cause double-free)
--FILE--
<?php
class InvalidDestructor {
public function __destruct() {
try {
$GLOBALS['obj']->setSize(0);
} catch (LogicException $ex) {
echo $ex->getMessage();
}
}
}

$obj = new SplFixedArray(1000);
$obj[0] = new InvalidDestructor();
$obj->setSize(0);
?>
--EXPECT--
recursive resize is not allowed