Skip to content

Commit b83421e

Browse files
committed
If the first index is negative let the next one stay negative
1 parent 2ed4723 commit b83421e

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

Zend/zend_hash.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht,
764764
}
765765
zend_hash_iterators_update(ht, HT_INVALID_IDX, h);
766766
if ((zend_long)h >= (zend_long)ht->nNextFreeElement) {
767-
ht->nNextFreeElement = h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
767+
ht->nNextFreeElement = (zend_long)h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
768768
}
769769
p->h = h;
770770
p->key = NULL;
@@ -786,7 +786,7 @@ static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht,
786786
}
787787
ZVAL_COPY_VALUE(&p->val, pData);
788788
if ((zend_long)h >= (zend_long)ht->nNextFreeElement) {
789-
ht->nNextFreeElement = h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
789+
ht->nNextFreeElement = (zend_long)h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
790790
}
791791
return &p->val;
792792
}
@@ -795,14 +795,18 @@ static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht,
795795
ZEND_HASH_IF_FULL_DO_RESIZE(ht); /* If the Hash table is full, resize it */
796796

797797
add_to_hash:
798+
/* If the first index is negative, dont force the next index to be 0 */
799+
if (UNEXPECTED((zend_long)h < 0 && ht->nNumOfElements == 0)) {
800+
ht->nNextFreeElement = h;
801+
}
798802
idx = ht->nNumUsed++;
799803
ht->nNumOfElements++;
800804
if (ht->nInternalPointer == HT_INVALID_IDX) {
801805
ht->nInternalPointer = idx;
802806
}
803807
zend_hash_iterators_update(ht, HT_INVALID_IDX, idx);
804808
if ((zend_long)h >= (zend_long)ht->nNextFreeElement) {
805-
ht->nNextFreeElement = h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
809+
ht->nNextFreeElement = (zend_long)h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
806810
}
807811
p = ht->arData + idx;
808812
p->h = h;

ext/standard/tests/array/array_fill_variation1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ array(2) {
120120
array(2) {
121121
[-10]=>
122122
int(100)
123-
[0]=>
123+
[-9]=>
124124
int(100)
125125
}
126126
-- Iteration 3 --

ext/standard/tests/array/array_fill_variation1_64bit.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ array(2) {
120120
array(2) {
121121
[-10]=>
122122
int(100)
123-
[0]=>
123+
[-9]=>
124124
int(100)
125125
}
126126
-- Iteration 3 --

0 commit comments

Comments
 (0)