Skip to content

Fix negative indices on empty array don't affect next chosen index #11157

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 7 commits 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
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ PHP 8.3 UPGRADE NOTES
inherited from the parent class. This will create a separate static property
storage for the current class. This is analogous to adding the static
property to the class directly without traits.
. Assigning a value to a negative index n in an empty array will now make
sure that the next index is n+1 and no longer default to 0.

- FFI:
. C functions that have a return type of void now return null instead of
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ ZEND_API const HashTable zend_empty_array = {
.nNumOfElements = 0,
.nTableSize = HT_MIN_SIZE,
.nInternalPointer = 0,
.nNextFreeElement = 0,
.nNextFreeElement = ZEND_LONG_MIN,
.pDestructor = ZVAL_PTR_DTOR
};

Expand Down
18 changes: 18 additions & 0 deletions ext/standard/tests/array/negative_index_empty_array.phpt
Original file line number Diff line number Diff line change
@@ -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"
}