-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
We address the `::setSize(0)` case by setting `array->element = NULL` before we destroy the elements, and bail in this case out on re-entry into `spl_fixedarray_resize()`.
Co-authored-by: Tyson Andre <tyson.andre@uwaterloo.ca>
The new test case (bug80663.phpt) was failing after the last commit; I've updated the test accordingly. Are we still good with this change for PHP-7.4? Also, there are relevant changes in PHP-8.0 (merge conflict); would the following patch be okay? ext/spl/spl_fixedarray.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/ext/spl/spl_fixedarray.c b/ext/spl/spl_fixedarray.c
index 4b3f0dcb80..cf4d371ade 100644
--- a/ext/spl/spl_fixedarray.c
+++ b/ext/spl/spl_fixedarray.c
@@ -171,8 +171,20 @@ static void spl_fixedarray_resize(spl_fixedarray *array, zend_long size)
/* clearing the array */
if (size == 0) {
- spl_fixedarray_dtor(array);
- array->elements = NULL;
+ if (array->elements != NULL) {
+ zend_long i;
+ zval *elements = array->elements;
+
+ array->elements = NULL;
+ array->size = 0;
+
+ for (i = 0; i < array->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);
spl_fixedarray_init_elems(array, array->size, size); This would avoid calling php-src/ext/spl/spl_fixedarray.c Lines 150 to 157 in 6144524
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR should change array->size to size, see above comment
We actually need to destroy all elements.
That was due to an erroneous change. This should be fixed now, but there's still the question how to solve this in PHP-8.0+. |
I'd suggest updating spl_fixedarray_dtor to do the early elements/size reset. |
Thanks. Done. |
We address the
::setSize(0)
case by settingarray->element = NULL
before we destroy the elements, and bail in this case out on re-entry
into
spl_fixedarray_resize()
.This is an alternative solution to PR #7485 based on @nikic's suggestion.