Skip to content

Commit 9741a9b

Browse files
committed
ext/pcre: simplify locale char table lookup and pattern info error handling
Use zend_hash_str_find_ptr/zend_hash_str_add_new_ptr for the locale character tables hash, avoiding a temporary zend_string allocation. Consolidate two pcre2_pattern_info calls with identical error handling into a single conditional.
1 parent f46bc8e commit 9741a9b

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

ext/pcre/php_pcre.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -750,9 +750,8 @@ 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));
753+
tables = (uint8_t *)zend_hash_str_find_ptr(&char_tables, ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)));
754754
if (!tables) {
755-
zend_string *_k;
756755
tables = pcre2_maketables(gctx);
757756
if (UNEXPECTED(!tables)) {
758757
php_error_docref(NULL,E_WARNING, "Failed to generate locale character tables");
@@ -761,10 +760,8 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
761760
efree(pattern);
762761
return NULL;
763762
}
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);
763+
764+
zend_hash_str_add_new_ptr(&char_tables, ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)), (void *)tables);
768765
}
769766
}
770767
pcre2_set_character_tables(cctx, tables);
@@ -826,19 +823,8 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
826823
new_entry.refcount = 0;
827824
new_entry.subpats_table = NULL;
828825

829-
rc = pcre2_pattern_info(re, PCRE2_INFO_CAPTURECOUNT, &new_entry.capture_count);
830-
if (rc < 0) {
831-
if (key != regex) {
832-
zend_string_release_ex(key, 0);
833-
}
834-
pcre2_code_free(new_entry.re);
835-
php_error_docref(NULL, E_WARNING, "Internal pcre2_pattern_info() error %d", rc);
836-
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
837-
return NULL;
838-
}
839-
840-
rc = pcre2_pattern_info(re, PCRE2_INFO_NAMECOUNT, &new_entry.name_count);
841-
if (rc < 0) {
826+
if ((rc = pcre2_pattern_info(re, PCRE2_INFO_CAPTURECOUNT, &new_entry.capture_count)) < 0 ||
827+
(rc = pcre2_pattern_info(re, PCRE2_INFO_NAMECOUNT, &new_entry.name_count)) < 0) {
842828
if (key != regex) {
843829
zend_string_release_ex(key, 0);
844830
}

0 commit comments

Comments
 (0)