Skip to content

Commit 3de8e64

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

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP NEWS
2222
. Fixed bug GH-22779 (mb_strrpos() returns the wrong position for a negative
2323
offset in a non-UTF-8 encoding). (Eyüp Can Akman)
2424

25+
- PCRE:
26+
. Fixed bug GH-21134 (Crash with \C + UTF-8). Using \C in UTF-8 patterns is
27+
now forbidden. (Arnaud)
28+
2529
- Sockets:
2630
. Fixed socket_set_option() validation error messages for UDP_SEGMENT and
2731
SO_LINGER options. (Weilin Du)

ext/pcre/php_pcre.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,8 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
725725
#ifdef PCRE2_UCP
726726
coptions |= PCRE2_UCP;
727727
#endif
728+
/* The \C escape sequence is unsafe in PCRE2_UTF mode */
729+
coptions |= PCRE2_NEVER_BACKSLASH_C;
728730
break;
729731
case 'J': coptions |= PCRE2_DUPNAMES; break;
730732

@@ -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)