Skip to content

Commit a7e0ddb

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Forbid \C in UTF-8 patterns (#21139)
2 parents 1fd0c64 + 3de8e64 commit a7e0ddb

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

ext/pcre/php_pcre.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
723723
#ifdef PCRE2_UCP
724724
coptions |= PCRE2_UCP;
725725
#endif
726+
/* The \C escape sequence is unsafe in PCRE2_UTF mode */
727+
coptions |= PCRE2_NEVER_BACKSLASH_C;
726728
break;
727729
case 'J': coptions |= PCRE2_DUPNAMES; break;
728730

@@ -778,8 +780,13 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
778780
if (key != regex) {
779781
zend_string_release_ex(key, 0);
780782
}
781-
pcre2_get_error_message(errnumber, error, sizeof(error));
782-
php_error_docref(NULL,E_WARNING, "Compilation failed: %s at offset %zu", error, erroffset);
783+
const char *err_msg = (const char*) error;
784+
if (errnumber == PCRE2_ERROR_BACKSLASH_C_CALLER_DISABLED) {
785+
err_msg = "using \\C is incompatible with the 'u' modifier";
786+
} else {
787+
pcre2_get_error_message(errnumber, error, sizeof(error));
788+
}
789+
php_error_docref(NULL,E_WARNING, "Compilation failed: %s at offset %zu", err_msg, erroffset);
783790
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
784791
efree(pattern);
785792
return NULL;

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)