Skip to content

Commit b690258

Browse files
committed
standard: Avoid double hash table lookup in iptcparse()
Closes GH-20420.
1 parent 0af87e9 commit b690258

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

UPGRADING.INTERNALS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PHP 8.6 INTERNALS UPGRADE NOTES
4444
. The zend_get_call_trampoline_func() API now takes the __call or
4545
__callStatic zend_function* instead of a CE and a boolean argument.
4646
. The zend_set_hash_symbol() API has been removed.
47+
. Added zend_hash_str_lookup().
4748

4849
========================
4950
2. Build system changes

Zend/zend_hash.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,13 @@ ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *st
10621062
return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD_NEW);
10631063
}
10641064

1065+
ZEND_API zval* ZEND_FASTCALL zend_hash_str_lookup(HashTable *ht, const char *str, size_t len)
1066+
{
1067+
zend_ulong h = zend_hash_func(str, len);
1068+
1069+
return _zend_hash_str_add_or_update_i(ht, str, len, h, NULL, HASH_LOOKUP);
1070+
}
1071+
10651072
ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h)
10661073
{
10671074
zval dummy;

Zend/zend_hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ static zend_always_inline zval *zend_hash_find_ex(const HashTable *ht, zend_stri
218218
/* Find or add NULL, if doesn't exist */
219219
ZEND_API zval* ZEND_FASTCALL zend_hash_lookup(HashTable *ht, zend_string *key);
220220
ZEND_API zval* ZEND_FASTCALL zend_hash_index_lookup(HashTable *ht, zend_ulong h);
221+
ZEND_API zval* ZEND_FASTCALL zend_hash_str_lookup(HashTable *ht, const char *str, size_t len);
221222

222223
#define ZEND_HASH_INDEX_LOOKUP(_ht, _h, _ret) do { \
223224
if (EXPECTED(HT_IS_PACKED(_ht))) { \

ext/standard/iptc.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ PHP_FUNCTION(iptcparse)
308308
unsigned char *buffer, recnum, dataset;
309309
char *str, key[16];
310310
size_t str_len;
311-
zval values, *element;
312311

313312
ZEND_PARSE_PARAMETERS_START(1, 1)
314313
Z_PARAM_STRING(str, str_len)
@@ -357,10 +356,9 @@ PHP_FUNCTION(iptcparse)
357356
array_init(return_value);
358357
}
359358

360-
if ((element = zend_hash_str_find(Z_ARRVAL_P(return_value), key, strlen(key))) == NULL) {
361-
array_init(&values);
362-
363-
element = zend_hash_str_update(Z_ARRVAL_P(return_value), key, strlen(key), &values);
359+
zval *element = zend_hash_str_lookup(Z_ARRVAL_P(return_value), key, strlen(key));
360+
if (Z_ISNULL_P(element)) {
361+
array_init(element);
364362
}
365363

366364
add_next_index_stringl(element, (char *) buffer+inx, len);

0 commit comments

Comments
 (0)