Skip to content

Commit bbaa557

Browse files
committed
ext/pcre: use zend_hash_str_lookup for locale char table management
Replace the separate zend_hash_find_ptr + zend_string_init + zend_hash_add_ptr + zend_string_release sequence with a single zend_hash_str_lookup() call which handles find-or-insert in one hash traversal and manages persistent key creation internally.
1 parent cbad7bf commit bbaa557

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

ext/pcre/php_pcre.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -750,21 +750,23 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
750750
}
751751

752752
if (key != regex) {
753-
tables = (uint8_t *)zend_hash_find_ptr(&char_tables, BG(ctype_string));
754-
if (!tables) {
755-
zend_string *_k;
753+
zv = zend_hash_str_lookup(&char_tables, ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)));
754+
if (Z_ISNULL_P(zv)) {
756755
tables = pcre2_maketables(gctx);
757756
if (UNEXPECTED(!tables)) {
757+
/* Remove the placeholder entry created by zend_hash_str_lookup(),
758+
* set ptr to NULL first so the destructor (pefree) is safe. */
759+
ZVAL_PTR(zv, NULL);
760+
zend_hash_str_del(&char_tables, ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)));
758761
php_error_docref(NULL,E_WARNING, "Failed to generate locale character tables");
759762
pcre_handle_exec_error(PCRE2_ERROR_NOMEMORY);
760763
zend_string_release_ex(key, 0);
761764
efree(pattern);
762765
return NULL;
763766
}
764-
_k = zend_string_init(ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)), 1);
765-
GC_MAKE_PERSISTENT_LOCAL(_k);
766-
zend_hash_add_ptr(&char_tables, _k, (void *)tables);
767-
zend_string_release(_k);
767+
ZVAL_PTR(zv, (void *)tables);
768+
} else {
769+
tables = Z_PTR_P(zv);
768770
}
769771
}
770772
pcre2_set_character_tables(cctx, tables);

0 commit comments

Comments
 (0)