Skip to content

Commit e2f477c

Browse files
ColinHDeviluuu1994
authored andcommitted
Fix negative indices on empty array not affecting next chosen index
Changed the value of nNextFreeElement in _zend_empty_array from 0 to ZEND_LONG_MIN. Fixes GH-11154 Closes GH-11157
1 parent f127e65 commit e2f477c

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ PHP NEWS
3131
values). (dstogov, nielsdos, ilutov)
3232
. Fix bug GH-10935 (Use of trait doesn't redeclare static property if class
3333
has inherited it from its parent). (ilutov)
34+
. Fix bug GH-11154 (Negative indices on empty array don't affect next chosen
35+
index). (ColinHDev)
3436

3537
- Date:
3638
. Implement More Appropriate Date/Time Exceptions RFC. (Derick)

UPGRADING

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ PHP 8.3 UPGRADE NOTES
3939
inherited from the parent class. This will create a separate static property
4040
storage for the current class. This is analogous to adding the static
4141
property to the class directly without traits.
42+
. Assigning a negative index n to an empty array will now make sure that the
43+
next index is n+1 instead of 0.
4244

4345
- FFI:
4446
. C functions that have a return type of void now return null instead of

Zend/zend_hash.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ ZEND_API const HashTable zend_empty_array = {
255255
.nNumOfElements = 0,
256256
.nTableSize = HT_MIN_SIZE,
257257
.nInternalPointer = 0,
258-
.nNextFreeElement = 0,
258+
.nNextFreeElement = ZEND_LONG_MIN,
259259
.pDestructor = ZVAL_PTR_DTOR
260260
};
261261

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test empty arrays with first added index being negative
3+
--FILE--
4+
<?php
5+
6+
$a = [];
7+
$a[-5] = "-5";
8+
$a[] = "after -5";
9+
10+
var_dump($a);
11+
?>
12+
--EXPECT--
13+
array(2) {
14+
[-5]=>
15+
string(2) "-5"
16+
[-4]=>
17+
string(8) "after -5"
18+
}

0 commit comments

Comments
 (0)