php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch pcre-crashes-fix.diff for PCRE related Bug #74403

Patch version 2017-04-10 11:09 UTC

Return to Bug #74403 | Download this patch
Patch Revisions:

Developer: tony2001@php.net

diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c
index 24b60eb..221de5c 100644
--- a/ext/pcre/php_pcre.c
+++ b/ext/pcre/php_pcre.c
@@ -578,6 +578,12 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
 		return NULL;
 	}
 
+#if HAVE_SETLOCALE
+	if (key != regex) {
+		zend_string_release(key);
+	}
+#endif
+
 	/*
 	 * Interned strings are not duplicated when stored in HashTable,
 	 * but all the interned strings created during HTTP request are removed
@@ -586,18 +592,16 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
 	 * as hash keys especually for this table.
 	 * See bug #63180
 	 */
-	if (!ZSTR_IS_INTERNED(key) || !(GC_FLAGS(key) & IS_STR_PERMANENT)) {
-		pce = zend_hash_str_update_mem(&PCRE_G(pcre_cache),
-				ZSTR_VAL(key), ZSTR_LEN(key), &new_entry, sizeof(pcre_cache_entry));
-#if HAVE_SETLOCALE
-		if (key != regex) {
-			zend_string_release(key);
-		}
-#endif
-	} else {
-		pce = zend_hash_update_mem(&PCRE_G(pcre_cache), key, &new_entry, sizeof(pcre_cache_entry));
+
+	if (!ZSTR_IS_INTERNED(regex) || !(GC_FLAGS(regex) & IS_STR_PERMANENT)) {
+		zend_string *str = zend_string_init(ZSTR_VAL(regex), ZSTR_LEN(regex), 1);
+		GC_REFCOUNT(str) = 0; /* will be incremented by zend_hash_update_mem() */
+		ZSTR_H(str) = ZSTR_H(regex);
+		regex = str;
 	}
 
+	pce = zend_hash_update_mem(&PCRE_G(pcre_cache), regex, &new_entry, sizeof(pcre_cache_entry));
+
 	return pce;
 }
 /* }}} */
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 03:01:27 2024 UTC