Skip to content

Implement the negative_array_index RFC #3772

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 1 commit 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
16 changes: 10 additions & 6 deletions Zend/zend_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ static zend_always_inline void _zend_hash_init_int(HashTable *ht, uint32_t nSize
ht->nNumUsed = 0;
ht->nNumOfElements = 0;
ht->nInternalPointer = 0;
ht->nNextFreeElement = 0;
ht->nNextFreeElement = ZEND_LONG_MIN;
ht->pDestructor = pDestructor;
ht->nTableSize = zend_hash_check_size(nSize);
}
Expand Down Expand Up @@ -935,6 +935,10 @@ static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht,
IS_CONSISTENT(ht);
HT_ASSERT_RC1(ht);

if (h == ZEND_LONG_MIN && (flag & HASH_ADD_NEXT)) {
h = 0;
}

if (HT_FLAGS(ht) & HASH_FLAG_PACKED) {
if (h < ht->nNumUsed) {
p = ht->arData + h;
Expand Down Expand Up @@ -998,8 +1002,8 @@ static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht,
p = ht->arData + idx;
Z_NEXT(p->val) = HT_HASH(ht, nIndex);
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
if ((zend_long)h >= (zend_long)ht->nNextFreeElement) {
ht->nNextFreeElement = h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
if ((zend_long)h >= ht->nNextFreeElement) {
ht->nNextFreeElement = (zend_long)h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
}
add:
ht->nNumOfElements++;
Expand Down Expand Up @@ -1665,7 +1669,7 @@ ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
}
ht->nNumUsed = 0;
ht->nNumOfElements = 0;
ht->nNextFreeElement = 0;
ht->nNextFreeElement = ZEND_LONG_MIN;
ht->nInternalPointer = 0;
}

Expand Down Expand Up @@ -1704,7 +1708,7 @@ ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht)
}
ht->nNumUsed = 0;
ht->nNumOfElements = 0;
ht->nNextFreeElement = 0;
ht->nNextFreeElement = ZEND_LONG_MIN;
ht->nInternalPointer = 0;
}

Expand Down Expand Up @@ -2018,7 +2022,7 @@ ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source)
target->nTableMask = HT_MIN_MASK;
target->nNumUsed = 0;
target->nNumOfElements = 0;
target->nNextFreeElement = 0;
target->nNextFreeElement = ZEND_LONG_MIN;
target->nInternalPointer = 0;
target->nTableSize = HT_MIN_SIZE;
HT_SET_DATA_ADDR(target, &uninitialized_bucket);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3228,7 +3228,7 @@ PHP_FUNCTION(array_pop)
}
ZVAL_COPY_DEREF(return_value, val);

if (!p->key && Z_ARRVAL_P(stack)->nNextFreeElement > 0 && p->h >= (zend_ulong)(Z_ARRVAL_P(stack)->nNextFreeElement - 1)) {
if (!p->key && (zend_long)p->h == (Z_ARRVAL_P(stack)->nNextFreeElement - 1)) {
Z_ARRVAL_P(stack)->nNextFreeElement = Z_ARRVAL_P(stack)->nNextFreeElement - 1;
}

Expand Down
4 changes: 2 additions & 2 deletions ext/standard/tests/array/bug67693.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ echo"\nDone";
?>
--EXPECT--
array(2) {
[0]=>
[-1]=>
int(0)
[1]=>
[0]=>
int(0)
}

Expand Down
31 changes: 31 additions & 0 deletions ext/standard/tests/array/negative_index.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Test arrays starting with negative indices
--FILE--
<?php

$a = array_fill(-2, 3, true);
$b = [-2 => true, true, true];
$c = ["string" => true, -2 => true, true, true];
unset($c["string"]);
$d[-2] = true;
$d[] = true;
$d[] = true;
$e = [-2 => false];
array_pop($e);
$e[] = true;
$e[] = true;
$e[] = true;

var_dump($a === $b && $b === $c && $c === $d && $d == $e);
var_dump($a);
?>
--EXPECT--
bool(true)
array(3) {
[-2]=>
bool(true)
[-1]=>
bool(true)
[0]=>
bool(true)
}