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
This commit is contained in:
ColinHDev 2023-04-28 17:12:31 +02:00 committed by Ilija Tovilo
parent f127e6581a
commit e2f477c2cb
No known key found for this signature in database
GPG Key ID: A4F5D403F118200A
4 changed files with 23 additions and 1 deletions

2
NEWS
View File

@ -31,6 +31,8 @@ PHP NEWS
values). (dstogov, nielsdos, ilutov) values). (dstogov, nielsdos, ilutov)
. Fix bug GH-10935 (Use of trait doesn't redeclare static property if class . Fix bug GH-10935 (Use of trait doesn't redeclare static property if class
has inherited it from its parent). (ilutov) has inherited it from its parent). (ilutov)
. Fix bug GH-11154 (Negative indices on empty array don't affect next chosen
index). (ColinHDev)
- Date: - Date:
. Implement More Appropriate Date/Time Exceptions RFC. (Derick) . Implement More Appropriate Date/Time Exceptions RFC. (Derick)

View File

@ -39,6 +39,8 @@ PHP 8.3 UPGRADE NOTES
inherited from the parent class. This will create a separate static property inherited from the parent class. This will create a separate static property
storage for the current class. This is analogous to adding the static storage for the current class. This is analogous to adding the static
property to the class directly without traits. property to the class directly without traits.
. Assigning a negative index n to an empty array will now make sure that the
next index is n+1 instead of 0.
- FFI: - FFI:
. C functions that have a return type of void now return null instead of . C functions that have a return type of void now return null instead of

View File

@ -255,7 +255,7 @@ ZEND_API const HashTable zend_empty_array = {
.nNumOfElements = 0, .nNumOfElements = 0,
.nTableSize = HT_MIN_SIZE, .nTableSize = HT_MIN_SIZE,
.nInternalPointer = 0, .nInternalPointer = 0,
.nNextFreeElement = 0, .nNextFreeElement = ZEND_LONG_MIN,
.pDestructor = ZVAL_PTR_DTOR .pDestructor = ZVAL_PTR_DTOR
}; };

View File

@ -0,0 +1,18 @@
--TEST--
Test empty arrays with first added index being negative
--FILE--
<?php
$a = [];
$a[-5] = "-5";
$a[] = "after -5";
var_dump($a);
?>
--EXPECT--
array(2) {
[-5]=>
string(2) "-5"
[-4]=>
string(8) "after -5"
}