Skip to content

Commit 99838b0

Browse files
committed
Store JIT/non-JIT regex in a different PCRE cache slot
1 parent 6e04050 commit 99838b0

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

ext/pcre/php_pcre.c

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -612,20 +612,31 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
612612
pcre_cache_entry *ret;
613613

614614
if (locale_aware && BG(ctype_string)) {
615-
key = zend_string_concat2(
615+
key = zend_string_concat3(
616616
ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)),
617-
ZSTR_VAL(regex), ZSTR_LEN(regex));
617+
ZSTR_VAL(regex), ZSTR_LEN(regex),
618+
#ifdef HAVE_PCRE_JIT_SUPPORT
619+
PCRE_G(jit) ? "1" : "0", 1
620+
#else
621+
"0", 1
622+
#endif
623+
);
618624
} else {
619-
key = regex;
625+
key = zend_string_concat2(
626+
ZSTR_VAL(regex), ZSTR_LEN(regex),
627+
#ifdef HAVE_PCRE_JIT_SUPPORT
628+
PCRE_G(jit) ? "1" : "0", 1
629+
#else
630+
"0", 1
631+
#endif
632+
);
620633
}
621634

622635
/* Try to lookup the cached regex entry, and if successful, just pass
623636
back the compiled pattern, otherwise go on and compile it. */
624637
zv = zend_hash_find(&PCRE_G(pcre_cache), key);
625638
if (zv) {
626-
if (key != regex) {
627-
zend_string_release_ex(key, 0);
628-
}
639+
zend_string_release_ex(key, 0);
629640
return (pcre_cache_entry*)Z_PTR_P(zv);
630641
}
631642

@@ -636,9 +647,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
636647
get to the end without encountering a delimiter. */
637648
while (isspace((int)*(unsigned char *)p)) p++;
638649
if (p >= end_p) {
639-
if (key != regex) {
640-
zend_string_release_ex(key, 0);
641-
}
650+
zend_string_release_ex(key, 0);
642651
php_error_docref(NULL, E_WARNING, "Empty regular expression");
643652
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
644653
return NULL;
@@ -648,9 +657,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
648657
or a backslash. */
649658
delimiter = *p++;
650659
if (isalnum((int)*(unsigned char *)&delimiter) || delimiter == '\\' || delimiter == '\0') {
651-
if (key != regex) {
652-
zend_string_release_ex(key, 0);
653-
}
660+
zend_string_release_ex(key, 0);
654661
php_error_docref(NULL, E_WARNING, "Delimiter must not be alphanumeric, backslash, or NUL");
655662
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
656663
return NULL;
@@ -691,9 +698,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
691698
}
692699

693700
if (pp >= end_p) {
694-
if (key != regex) {
695-
zend_string_release_ex(key, 0);
696-
}
701+
zend_string_release_ex(key, 0);
697702
if (start_delimiter == end_delimiter) {
698703
php_error_docref(NULL,E_WARNING, "No ending delimiter '%c' found", delimiter);
699704
} else {
@@ -753,9 +758,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
753758
}
754759
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
755760
efree(pattern);
756-
if (key != regex) {
757-
zend_string_release_ex(key, 0);
758-
}
761+
zend_string_release_ex(key, 0);
759762
return NULL;
760763
}
761764
}
@@ -764,13 +767,11 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
764767
php_error_docref(NULL, E_WARNING, "The /e modifier is no longer supported, use preg_replace_callback instead");
765768
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
766769
efree(pattern);
767-
if (key != regex) {
768-
zend_string_release_ex(key, 0);
769-
}
770+
zend_string_release_ex(key, 0);
770771
return NULL;
771772
}
772773

773-
if (key != regex) {
774+
if (locale_aware && BG(ctype_string)) {
774775
tables = (uint8_t *)zend_hash_find_ptr(&char_tables, BG(ctype_string));
775776
if (!tables) {
776777
zend_string *_k;
@@ -794,9 +795,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
794795
re = pcre2_compile((PCRE2_SPTR)pattern, pattern_len, coptions, &errnumber, &erroffset, cctx);
795796

796797
if (re == NULL) {
797-
if (key != regex) {
798-
zend_string_release_ex(key, 0);
799-
}
798+
zend_string_release_ex(key, 0);
800799
pcre2_get_error_message(errnumber, error, sizeof(error));
801800
php_error_docref(NULL,E_WARNING, "Compilation failed: %s at offset %zu", error, erroffset);
802801
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
@@ -846,19 +845,15 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
846845

847846
rc = pcre2_pattern_info(re, PCRE2_INFO_CAPTURECOUNT, &new_entry.capture_count);
848847
if (rc < 0) {
849-
if (key != regex) {
850-
zend_string_release_ex(key, 0);
851-
}
848+
zend_string_release_ex(key, 0);
852849
php_error_docref(NULL, E_WARNING, "Internal pcre2_pattern_info() error %d", rc);
853850
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
854851
return NULL;
855852
}
856853

857854
rc = pcre2_pattern_info(re, PCRE2_INFO_NAMECOUNT, &new_entry.name_count);
858855
if (rc < 0) {
859-
if (key != regex) {
860-
zend_string_release_ex(key, 0);
861-
}
856+
zend_string_release_ex(key, 0);
862857
php_error_docref(NULL, E_WARNING, "Internal pcre_pattern_info() error %d", rc);
863858
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
864859
return NULL;
@@ -882,9 +877,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
882877
ret = zend_hash_add_new_mem(&PCRE_G(pcre_cache), key, &new_entry, sizeof(pcre_cache_entry));
883878
}
884879

885-
if (key != regex) {
886-
zend_string_release_ex(key, 0);
887-
}
880+
zend_string_release_ex(key, 0);
888881

889882
return ret;
890883
}

0 commit comments

Comments
 (0)