Skip to content

Fix number of elements after packed hash filling #11022

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

Merged
merged 1 commit into from
Apr 6, 2023
Merged
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
4 changes: 2 additions & 2 deletions Zend/zend_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -1517,8 +1517,8 @@ static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht,

#define ZEND_HASH_FILL_GROW() do { \
if (UNEXPECTED(__fill_idx >= __fill_ht->nTableSize)) { \
__fill_ht->nNumOfElements += __fill_idx - __fill_ht->nNumUsed; \
__fill_ht->nNumUsed = __fill_idx; \
__fill_ht->nNumOfElements = __fill_idx; \
__fill_ht->nNextFreeElement = __fill_idx; \
zend_hash_packed_grow(__fill_ht); \
__fill_val = __fill_ht->arPacked + __fill_idx; \
Expand Down Expand Up @@ -1557,8 +1557,8 @@ static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht,
} while (0)

#define ZEND_HASH_FILL_FINISH() do { \
__fill_ht->nNumOfElements += __fill_idx - __fill_ht->nNumUsed; \
__fill_ht->nNumUsed = __fill_idx; \
__fill_ht->nNumOfElements = __fill_idx; \
__fill_ht->nNextFreeElement = __fill_idx; \
__fill_ht->nInternalPointer = 0; \
} while (0)
Expand Down
23 changes: 23 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,29 @@ static ZEND_FUNCTION(zend_get_map_ptr_last)
RETURN_LONG(CG(map_ptr_last));
}

static ZEND_FUNCTION(zend_test_fill_packed_array)
{
HashTable *parameter;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_HT_EX(parameter, 0, 1)
ZEND_PARSE_PARAMETERS_END();

if (!HT_IS_PACKED(parameter)) {
zend_argument_value_error(1, "must be a packed array");
RETURN_THROWS();
}

zend_hash_extend(parameter, parameter->nNumUsed + 10, true);
ZEND_HASH_FILL_PACKED(parameter) {
for (int i = 0; i < 10; i++) {
zval value;
ZVAL_LONG(&value, i);
ZEND_HASH_FILL_ADD(&value);
}
} ZEND_HASH_FILL_END();
}

static zend_object *zend_test_class_new(zend_class_entry *class_type)
{
zend_object *obj = zend_objects_new(class_type);
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ function zend_test_zend_call_stack_use_all(): int {}
function zend_test_is_string_marked_as_valid_utf8(string $string): bool {}

function zend_get_map_ptr_last(): int {}

function zend_test_fill_packed_array(array &$array): void {}
}

namespace ZendTestNS {
Expand Down
8 changes: 7 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions ext/zend_test/tests/hash_fill_packed_nr_elements.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
Test hash packed fill number of elements
--EXTENSIONS--
zend_test
--FILE--
<?php

function number() {
return 6;
}

$my_array = [number() => 0];
zend_test_fill_packed_array($my_array);

var_dump($my_array);
var_dump(count($my_array));

?>
--EXPECT--
array(11) {
[6]=>
int(0)
[7]=>
int(0)
[8]=>
int(1)
[9]=>
int(2)
[10]=>
int(3)
[11]=>
int(4)
[12]=>
int(5)
[13]=>
int(6)
[14]=>
int(7)
[15]=>
int(8)
[16]=>
int(9)
}
int(11)