Skip to content
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
8 changes: 5 additions & 3 deletions Zend/zend_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static zend_always_inline uint32_t zend_hash_check_size(uint32_t nSize)
static zend_always_inline void zend_hash_real_init_packed_ex(HashTable *ht)
{
void *data;
uint8_t prev_flags = HT_FLAGS(ht) & HASH_FLAG_ALLOW_COW_VIOLATION;

if (UNEXPECTED(GC_FLAGS(ht) & IS_ARRAY_PERSISTENT)) {
data = pemalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK), 1);
Expand All @@ -160,14 +161,15 @@ static zend_always_inline void zend_hash_real_init_packed_ex(HashTable *ht)
}
HT_SET_DATA_ADDR(ht, data);
/* Don't overwrite iterator count. */
ht->u.v.flags = HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
ht->u.v.flags = prev_flags | HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
HT_HASH_RESET_PACKED(ht);
}

static zend_always_inline void zend_hash_real_init_mixed_ex(HashTable *ht)
{
void *data;
uint32_t nSize = ht->nTableSize;
uint8_t prev_flags = HT_FLAGS(ht) & HASH_FLAG_ALLOW_COW_VIOLATION;

ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));

Expand All @@ -178,7 +180,7 @@ static zend_always_inline void zend_hash_real_init_mixed_ex(HashTable *ht)
ht->nTableMask = HT_SIZE_TO_MASK(HT_MIN_SIZE);
HT_SET_DATA_ADDR(ht, data);
/* Don't overwrite iterator count. */
ht->u.v.flags = HASH_FLAG_STATIC_KEYS;
ht->u.v.flags = prev_flags | HASH_FLAG_STATIC_KEYS;
#if defined(__AVX2__)
do {
__m256i ymm0 = _mm256_setzero_si256();
Expand Down Expand Up @@ -227,7 +229,7 @@ static zend_always_inline void zend_hash_real_init_mixed_ex(HashTable *ht)
}
ht->nTableMask = HT_SIZE_TO_MASK(nSize);
HT_SET_DATA_ADDR(ht, data);
HT_FLAGS(ht) = HASH_FLAG_STATIC_KEYS;
HT_FLAGS(ht) = prev_flags | HASH_FLAG_STATIC_KEYS;
HT_HASH_RESET(ht);
}

Expand Down
18 changes: 18 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ static zend_class_entry *zend_test_int_enum;
static zend_class_entry *zend_test_magic_call;
static zend_object_handlers zend_test_class_handlers;

static ZEND_FUNCTION(zend_test_gh12986)
{
ZEND_PARSE_PARAMETERS_NONE();
HashTable *ht = _zend_new_array_0();
// The HT_ALLOW_COW_VIOLATION() macro is a noop in non-debug builds,
// so we have to set the flag directly.
HT_FLAGS(ht) |= HASH_FLAG_ALLOW_COW_VIOLATION;
zval value;
ZVAL_LONG(&value, 1);
zend_hash_next_index_insert(ht, &value);
if (HT_FLAGS(ht) & HASH_FLAG_ALLOW_COW_VIOLATION) {
php_printf("OK: HASH_FLAG_ALLOW_COW_VIOLATION is preserved by zend_hash_real_init_ex\n");
} else {
php_printf("KO: HASH_FLAG_ALLOW_COW_VIOLATION was cleared by zend_hash_real_init_ex\n");
}
zend_array_release(ht);
}

static int le_throwing_resource;

static ZEND_FUNCTION(zend_test_func)
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 @@ -246,6 +246,8 @@ function zend_test_override_libxml_global_state(): void {}
#endif

function zend_test_is_pcre_bundled(): bool {}

function zend_test_gh12986(): void {}
}

namespace ZendTestNS {
Expand Down
6 changes: 5 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.

12 changes: 12 additions & 0 deletions ext/zend_test/tests/gh12986.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
GH-12986 (HASH_FLAG_ALLOW_COW_VIOLATION is not preserved by zend_hash_real_init_ex)
--EXTENSIONS--
zend_test
--FILE--
<?php

zend_test_gh12986();

?>
--EXPECT--
OK: HASH_FLAG_ALLOW_COW_VIOLATION is preserved by zend_hash_real_init_ex