Skip to content

Commit

Permalink
Fix GH-13531: Unable to resize SplfixedArray after being unserialized…
Browse files Browse the repository at this point in the history
… in PHP 8.2.15

When unserializing, the cached_resize field was not reset to -1
correctly, causing the setSize() method to think we were inside of a
resize operation.

Closes GH-13543.
  • Loading branch information
nielsdos committed Feb 27, 2024
1 parent 3d4b36f commit 8494058
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -5,6 +5,10 @@ PHP NEWS
- PDO:
. Fix various PDORow bugs. (Girgias)

- SPL:
. Fixed bug GH-13531 (Unable to resize SplfixedArray after being unserialized
in PHP 8.2.15). (nielsdos)

- XML:
. Fixed bug GH-13517 (Multiple test failures when building with
--with-expat). (nielsdos)
Expand Down
3 changes: 2 additions & 1 deletion ext/spl/spl_fixedarray.c
Expand Up @@ -91,6 +91,7 @@ static void spl_fixedarray_default_ctor(spl_fixedarray *array)
{
array->size = 0;
array->elements = NULL;
array->cached_resize = -1;
}

/* Initializes the range [from, to) to null. Does not dtor existing elements. */
Expand All @@ -110,6 +111,7 @@ static void spl_fixedarray_init_non_empty_struct(spl_fixedarray *array, zend_lon
array->elements = size ? safe_emalloc(size, sizeof(zval), 0) : NULL;
array->size = size;
array->should_rebuild_properties = true;
array->cached_resize = -1;
}

static void spl_fixedarray_init(spl_fixedarray *array, zend_long size)
Expand All @@ -120,7 +122,6 @@ static void spl_fixedarray_init(spl_fixedarray *array, zend_long size)
} else {
spl_fixedarray_default_ctor(array);
}
array->cached_resize = -1;
}

/* Copies the range [begin, end) into the fixedarray, beginning at `offset`.
Expand Down
28 changes: 28 additions & 0 deletions ext/spl/tests/gh13531.phpt
@@ -0,0 +1,28 @@
--TEST--
GH-13531 (Unable to resize SplfixedArray after being unserialized in PHP 8.2.15)
--FILE--
<?php

$array = new SplFixedArray(5);
$array[4] = 1;
$serialized = serialize($array);
$unserialized = unserialize($serialized);
$unserialized->setSize(6);
var_dump($unserialized);

?>
--EXPECT--
object(SplFixedArray)#2 (6) {
[0]=>
NULL
[1]=>
NULL
[2]=>
NULL
[3]=>
NULL
[4]=>
int(1)
[5]=>
NULL
}

0 comments on commit 8494058

Please sign in to comment.