Skip to content

Commit a58b494

Browse files
committed
Avoid a PCRE bug with a negated set of one character
When ignoring case, PCRE 10.32 to 10.39 with JIT can match only a part of a character with a negated set of one character whose letter has more than one other case, like "[^s]". On "日", it matched two of the three bytes, so REGEXP_SUBSTR() or REGEXP_REPLACE() could return invalid UTF-8. PHP 7.3 to 8.1 bundle these versions. Repeat the character in such a set. PCRE then compiles a class, which doesn't have the bug.
1 parent c17c0cf commit a58b494

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎packages/mysql-on-sqlite/src/icu/class-wp-icu-regex-translator.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,14 @@ function ( $parts ) use ( &$previous ) {
353353
},
354354
substr( $set, $start, -1 )
355355
);
356+
357+
/*
358+
* When ignoring case, PCRE 10.32 to 10.39 can match a part of a character with a
359+
* negated set of one character, like "[^s]". Repeating the character avoids that.
360+
*/
361+
if ( 2 === $start && preg_match( '/^(?:\\\\x\{[0-9A-F]+\}|\\\\[^a-zA-Z]|[^\\\\])$/su', $body ) ) {
362+
$body .= $body;
363+
}
356364
return substr( $set, 0, $start ) . $body . ']';
357365
}
358366

‎packages/mysql-on-sqlite/tests/icu/WP_ICU_Regex_Translator_Tests.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,16 @@ public function data_output(): array {
856856
);
857857
}
858858

859+
/**
860+
* When ignoring case, PCRE 10.32 to 10.39 match a part of a character with "[^s]".
861+
*/
862+
public function test_a_negated_set_of_one_character_matches_whole_characters(): void {
863+
$this->assertSame( '/[^ss]/uXi', WP_ICU_Regex_Translator::translate( '[^s]', 'i' ) );
864+
$this->assertSame( '<日><本>s<😀>', $this->replace_matches( WP_ICU_Regex_Translator::translate( '[^s]', 'i' ), '日本s😀' ) );
865+
$this->assertSame( '<“x>ς', $this->replace_matches( WP_ICU_Regex_Translator::translate( '[^σ]{2}', 'i' ), '“xς' ) );
866+
$this->assertSame( '<日>s', $this->replace_matches( WP_ICU_Regex_Translator::translate( '(?i)[^\x73]' ), '日s' ) );
867+
}
868+
859869
public function test_inline_flags_end_with_their_group(): void {
860870
$this->assertSame( 1, preg_match( WP_ICU_Regex_Translator::translate( '(?:(?s).)a' ), "\na" ) );
861871
$this->assertSame( 0, preg_match( WP_ICU_Regex_Translator::translate( '(?:(?s).).' ), "\n\n" ) );

0 commit comments

Comments
 (0)