Skip to content

Commit a14744c

Browse files
committed
Disable \C in UTF-8 patterns
1 parent b8fc6bd commit a14744c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

ext/pcre/php_pcre.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
734734
#ifdef PCRE2_UCP
735735
coptions |= PCRE2_UCP;
736736
#endif
737+
/* The \C escape sequence is unsafe in PCRE2_UTF mode */
738+
coptions |= PCRE2_NEVER_BACKSLASH_C;
737739
break;
738740
case 'J': coptions |= PCRE2_DUPNAMES; break;
739741

@@ -787,7 +789,11 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
787789
if (key != regex) {
788790
zend_string_release_ex(key, 0);
789791
}
790-
pcre2_get_error_message(errnumber, error, sizeof(error));
792+
if (errnumber == PCRE2_ERROR_BACKSLASH_C_CALLER_DISABLED) {
793+
strlcpy((char*)error, "using \\C is incompatible with the 'u' modifier", sizeof(error));
794+
} else {
795+
pcre2_get_error_message(errnumber, error, sizeof(error));
796+
}
791797
php_error_docref(NULL,E_WARNING, "Compilation failed: %s at offset %zu", error, erroffset);
792798
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
793799
efree(pattern);

ext/pcre/tests/gh21134.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-21134: ASan negative-size-param in preg_match_all() with \C + UTF-8 multibyte input
3+
--CREDITS--
4+
vi3tL0u1s
5+
--FILE--
6+
<?php
7+
8+
$r = preg_match_all("/(.*)\\C/u", "à", $m);
9+
var_dump($r, $m);
10+
11+
?>
12+
--EXPECTF--
13+
Warning: preg_match_all(): Compilation failed: using \C is incompatible with the 'u' modifier at offset 6 in %s on line %d
14+
bool(false)
15+
NULL

0 commit comments

Comments
 (0)