Skip to content

Commit e8aaacb

Browse files
committed
Translate ICU's code point escapes
ICU reads "\uhhhh" and "\Uhhhhhhhh" as code points, which PCRE rejects. It reads "\0ooo" as an octal code point with up to three digits, where PCRE reads up to two, so "\0101" matched "\010" and "1" instead of "A". Translate them to "\x{...}", also in sets. Like in ICU, a surrogate pair of "\u" escapes is one code point.
1 parent b4b75b3 commit e8aaacb

6 files changed

Lines changed: 82 additions & 430 deletions

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public static function register_for( $pdo ): self {
108108
'regexp_instr' => array( 2, 3, 4, 5, 6 ),
109109
);
110110

111+
/**
112+
* ICU escapes of a code point that PCRE lacks, or reads differently: "\uhhhh",
113+
* "\Uhhhhhhhh", and "\0ooo" up to "\0377". A surrogate pair of "\u" escapes is
114+
* one code point.
115+
*/
116+
private const REGEXP_CODE_POINT_ESCAPE = '\\\\u[dD][89abAB][0-9a-fA-F]{2}\\\\u[dD][c-fC-F][0-9a-fA-F]{2}|\\\\u[0-9a-fA-F]{4}|\\\\U[0-9a-fA-F]{8}|\\\\0(?:[0-3][0-7]{0,2}|[4-7][0-7]?)';
117+
111118
/** @var string[] Compiled PCRE patterns, keyed by byte mode, "\G" variant, match type, and pattern. */
112119
private $regexp_cache = array();
113120

@@ -1305,8 +1312,8 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
13051312

13061313
/*
13071314
* PHP 7 can read an unknown escape as literal text. For "\y", that matches ICU,
1308-
* but "\u0041" matches "u0041", where ICU matches "A". The "X" modifier makes
1309-
* all of them errors, like in PHP 8.
1315+
* but ICU rejects "\u004", which has too few digits. The "X" modifier makes all
1316+
* of them errors, like in PHP 8.
13101317
*/
13111318
$modifiers = $binary_mode ? 'X' : 'uX';
13121319
if ( ! $case_sensitive ) {
@@ -1418,7 +1425,7 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $without
14181425
* scan the rest of the pattern each time.
14191426
*/
14201427
$translated = preg_replace_callback(
1421-
'/\\\\Q.*?(?:\\\\E|\z)|(\[(?!:)\^?\]?(?:\\\\Q.*?(?:\\\\E|\z)|\\\\.|\[:\^?\w+:\]|(?1)|[^\\\\\[\]])*\])|\[:\^?\w+:\]|\\\\[pP]\{[^}\\\\]*\}|\\\\.|[\/\0]/s',
1428+
'/\\\\Q.*?(?:\\\\E|\z)|(\[(?!:)\^?\]?(?:\\\\Q.*?(?:\\\\E|\z)|\\\\.|\[:\^?\w+:\]|(?1)|[^\\\\\[\]])*\])|\[:\^?\w+:\]|' . self::REGEXP_CODE_POINT_ESCAPE . '|\\\\[pP]\{[^}\\\\]*\}|\\\\.|[\/\0]/s',
14221429
function ( $parts ) use ( $pattern, $tokens ) {
14231430
$token = $parts[0];
14241431
if ( isset( $parts[1] ) ) {
@@ -1460,7 +1467,7 @@ private function regexp_translate_set( $set, $pattern, $tokens ) {
14601467
// An escape with its argument is one token, and so is a character, also when the
14611468
// set isn't valid UTF-8.
14621469
preg_match_all(
1463-
'/\\\\Q.*?(?:\\\\E|\z)|\\\\(?:[pP](?:\{[^}\\\\]*\}|\w)|x(?:\{[^}\\\\]*\}|[0-9a-fA-F]{1,2})|N\{[^}\\\\]*\}|c.|.)|\[:\^?\w+:\]|\[\^?|&&|--|[\xC0-\xFF][\x80-\xBF]*|./s',
1470+
'/\\\\Q.*?(?:\\\\E|\z)|' . self::REGEXP_CODE_POINT_ESCAPE . '|\\\\(?:[pP](?:\{[^}\\\\]*\}|\w)|x(?:\{[^}\\\\]*\}|[0-9a-fA-F]{1,2})|N\{[^}\\\\]*\}|c.|.)|\[:\^?\w+:\]|\[\^?|&&|--|[\xC0-\xFF][\x80-\xBF]*|./s',
14641471
$set,
14651472
$parts
14661473
);
@@ -1634,7 +1641,21 @@ private function regexp_translate_token( $token, $tokens ) {
16341641
);
16351642
return '\\E' === substr( $token, -2 ) ? $quoted : $quoted . '\\E';
16361643
}
1637-
return $tokens[ $token ] ?? $token;
1644+
if ( isset( $tokens[ $token ] ) ) {
1645+
return $tokens[ $token ];
1646+
}
1647+
if ( ! preg_match( '/^(?:' . self::REGEXP_CODE_POINT_ESCAPE . ')$/', $token ) ) {
1648+
return $token;
1649+
}
1650+
if ( '0' === $token[1] ) {
1651+
$code = octdec( substr( $token, 2 ) );
1652+
} elseif ( 12 === strlen( $token ) ) {
1653+
// A surrogate pair of "\u" escapes.
1654+
$code = 0x10000 + ( ( hexdec( substr( $token, 2, 4 ) ) - 0xD800 ) << 10 ) + hexdec( substr( $token, 8, 4 ) ) - 0xDC00;
1655+
} else {
1656+
$code = hexdec( substr( $token, 2 ) );
1657+
}
1658+
return sprintf( '\\x{%X}', $code );
16381659
}
16391660

16401661
/**

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public static function regexpLikeErrors() {
382382
return array(
383383
'invalid pattern' => array( "REGEXP_LIKE('abc', '(abc')", 'Invalid regular expression: (abc.' ),
384384
'unknown escape' => array( "REGEXP_LIKE('y', '\\\\y')", 'Invalid regular expression: \\y.' ),
385-
'ICU escape' => array( "REGEXP_LIKE('A', '\\\\u0041')", 'Invalid regular expression: \\u0041.' ),
385+
'short escape' => array( "REGEXP_LIKE('u004', '\\\\u004')", 'Invalid regular expression: \\u004.' ),
386386
'bad property' => array( "REGEXP_LIKE('a', '\\\\p{Foo}')", 'Invalid regular expression: \\p{Foo}.' ),
387387
'operator escape' => array( "'y' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ),
388388
'binary escape' => array( "X'FF' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ),
@@ -1583,6 +1583,7 @@ public function testRegexpBigIntegerColumn() {
15831583

15841584
/**
15851585
* @dataProvider regexpSetCases
1586+
* @dataProvider regexpCodePointEscapeCases
15861587
*/
15871588
public function testRegexpIcuSyntax( $pattern, $subject, $expected ) {
15881589
$this->assertQuery(
@@ -1662,6 +1663,28 @@ public static function regexpSetCases() {
16621663
);
16631664
}
16641665

1666+
public static function regexpCodePointEscapeCases() {
1667+
return array(
1668+
'"\\u"' => array( '\\u0041', 'ABA', '<A>B<A>' ),
1669+
'"\\u" in a set' => array( '[\\u0041c]+', 'AcAcdef', '<AcAc>def' ),
1670+
'"\\u" range' => array( '[\\u0041-\\u0043]', 'ABCD', '<A><B><C>D' ),
1671+
'"\\u", 2 bytes' => array( 'caf\\u00e9', 'café', '<café>' ),
1672+
'"\\U"' => array( '\\U00010400', '𐐀', '<𐐀>' ),
1673+
'"\\U" range' => array( '[\\u0000-\\U0010ffff]+', 'a😀', '<a😀>' ),
1674+
'negated "\\U" range' => array( '[^\\u0000-\\U0010ffff]', 'a😀', 'a😀' ),
1675+
'surrogate pair' => array( '\\ud800\\udc00', 'a𐀀', 'a<𐀀>' ),
1676+
'surrogate pair, uppercase' => array( '\\uD800\\uDC00', 'a𐀀', 'a<𐀀>' ),
1677+
'escaped backslash' => array( '\\\\\\u0041', 'A', 'A' ),
1678+
'quoted' => array( '\\Q\\u0041\\E', 'u0041', 'u0041' ),
1679+
'octal' => array( '\\0101', 'ABA', '<A>B<A>' ),
1680+
'octal, 1 and 2 digits' => array( '\\0154\\01442', 'ld2', '<ld2>' ),
1681+
'octal up to 0377' => array( '\\0376', 'þ', '<þ>' ),
1682+
'octal past 0377' => array( '\\0400', ' 0', '< 0>' ),
1683+
'octal NUL' => array( '\\00', 'a', 'a' ),
1684+
'octal range' => array( '[\\0101-\\0103]', 'ABCD', '<A><B><C>D' ),
1685+
);
1686+
}
1687+
16651688
/**
16661689
* @dataProvider regexpInvalidSetCases
16671690
*/

‎packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Instr_Tests.php‎

Lines changed: 8 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,10 @@ protected function get_known_failures(): array {
174174
'icu-regextst:232',
175175
'icu-regextst:233',
176176
'icu-regextst:234',
177-
'icu-regextst:329',
178-
'icu-regextst:330',
179177
'icu-regextst:365',
180178
'icu-regextst:366',
181179
'icu-regextst:367',
182180
'icu-regextst:368',
183-
'icu-regextst:373',
184-
'icu-regextst:374',
185-
'icu-regextst:375',
186-
'icu-regextst:376',
187-
'icu-regextst:377',
188-
'icu-regextst:378',
189181
'icu-regextst:383',
190182
'icu-regextst:384',
191183
'icu-regextst:385',
@@ -210,10 +202,6 @@ protected function get_known_failures(): array {
210202
'icu-regextst:565',
211203
'icu-regextst:566',
212204
'icu-regextst:567',
213-
'icu-regextst:580',
214-
'icu-regextst:581',
215-
'icu-regextst:582',
216-
'icu-regextst:583',
217205
'icu-regextst:590',
218206
'icu-regextst:591',
219207
'icu-regextst:626',
@@ -224,18 +212,10 @@ protected function get_known_failures(): array {
224212
'icu-regextst:631',
225213
'icu-regextst:632',
226214
'icu-regextst:633',
227-
'icu-regextst:705',
228-
'icu-regextst:706',
229-
'icu-regextst:709',
230-
'icu-regextst:710',
231215
'icu-regextst:711',
232216
'icu-regextst:712',
233217
'icu-regextst:713',
234218
'icu-regextst:714',
235-
'icu-regextst:715',
236-
'icu-regextst:716',
237-
'icu-regextst:717',
238-
'icu-regextst:718',
239219
'icu-regextst:719',
240220
'icu-regextst:720',
241221
'icu-regextst:725',
@@ -244,8 +224,6 @@ protected function get_known_failures(): array {
244224
'icu-regextst:728',
245225
'icu-regextst:843',
246226
'icu-regextst:844',
247-
'icu-regextst:935',
248-
'icu-regextst:936',
249227
'icu-regextst:941',
250228
'icu-regextst:942',
251229
'icu-regextst:949',
@@ -352,8 +330,6 @@ protected function get_known_failures(): array {
352330
'icu-regextst:1242',
353331
'icu-regextst:1243',
354332
'icu-regextst:1244',
355-
'icu-regextst:1247',
356-
'icu-regextst:1248',
357333
'icu-regextst:1249',
358334
'icu-regextst:1250',
359335
'icu-regextst:1251',
@@ -376,8 +352,6 @@ protected function get_known_failures(): array {
376352
'icu-regextst:1272',
377353
'icu-regextst:1301',
378354
'icu-regextst:1302',
379-
'icu-regextst:1318',
380-
'icu-regextst:1319',
381355
'icu-regextst:1330',
382356
'icu-regextst:1331',
383357
'icu-regextst:1332',
@@ -388,10 +362,6 @@ protected function get_known_failures(): array {
388362
'icu-regextst:1337',
389363
'icu-regextst:1338',
390364
'icu-regextst:1339',
391-
'icu-regextst:1364',
392-
'icu-regextst:1365',
393-
'icu-regextst:1366',
394-
'icu-regextst:1367',
395365
'icu-regextst:1370',
396366
'icu-regextst:1371',
397367
'icu-regextst:1372',
@@ -480,90 +450,14 @@ protected function get_known_failures(): array {
480450
'icu-regextst:1457',
481451
'icu-regextst:1458',
482452
'icu-regextst:1459',
483-
'icu-regextst:1466',
484-
'icu-regextst:1467',
485-
'icu-regextst:1468',
486-
'icu-regextst:1469',
487-
'icu-regextst:1470',
488-
'icu-regextst:1471',
489-
'icu-regextst:1472',
490-
'icu-regextst:1473',
491-
'icu-regextst:1474',
492-
'icu-regextst:1475',
493-
'icu-regextst:1476',
494-
'icu-regextst:1477',
495-
'icu-regextst:1478',
496-
'icu-regextst:1479',
497-
'icu-regextst:1480',
498-
'icu-regextst:1481',
499-
'icu-regextst:1482',
500-
'icu-regextst:1483',
501-
'icu-regextst:1484',
502-
'icu-regextst:1485',
503-
'icu-regextst:1490',
504-
'icu-regextst:1491',
505453
'icu-regextst:1494',
506454
'icu-regextst:1495',
507455
'icu-regextst:1506',
508456
'icu-regextst:1507',
509457
'icu-regextst:1508',
510458
'icu-regextst:1509',
511-
'icu-regextst:2768',
512-
'icu-regextst:2769',
513-
'icu-regextst:2770',
514-
'icu-regextst:2771',
515-
'icu-regextst:2772',
516-
'icu-regextst:2773',
517-
'icu-regextst:2774',
518-
'icu-regextst:2775',
519-
'icu-regextst:2776',
520-
'icu-regextst:2777',
521-
'icu-regextst:2778',
522-
'icu-regextst:2779',
523-
'icu-regextst:3338',
524-
'icu-regextst:3339',
525-
'icu-regextst:3340',
526-
'icu-regextst:3341',
527-
'icu-regextst:3342',
528-
'icu-regextst:3343',
529-
'icu-regextst:3344',
530-
'icu-regextst:3345',
531-
'icu-regextst:3396',
532-
'icu-regextst:3397',
533-
'icu-regextst:3398',
534-
'icu-regextst:3399',
535-
'icu-regextst:3400',
536-
'icu-regextst:3401',
537-
'icu-regextst:3402',
538-
'icu-regextst:3403',
539459
'icu-regextst:3508',
540460
'icu-regextst:3509',
541-
'icu-regextst:3518',
542-
'icu-regextst:3519',
543-
'icu-regextst:3520',
544-
'icu-regextst:3521',
545-
'icu-regextst:3522',
546-
'icu-regextst:3523',
547-
'icu-regextst:3524',
548-
'icu-regextst:3525',
549-
'icu-regextst:3538',
550-
'icu-regextst:3539',
551-
'icu-regextst:3540',
552-
'icu-regextst:3541',
553-
'icu-regextst:3542',
554-
'icu-regextst:3543',
555-
'icu-regextst:3544',
556-
'icu-regextst:3545',
557-
'icu-regextst:3606',
558-
'icu-regextst:3607',
559-
'icu-regextst:3608',
560-
'icu-regextst:3609',
561-
'icu-regextst:3610',
562-
'icu-regextst:3611',
563-
'icu-regextst:3836',
564-
'icu-regextst:3837',
565-
'icu-regextst:3838',
566-
'icu-regextst:3839',
567461
);
568462

569463
// PCRE 8 rejects this nested lookbehind as not fixed in length.
@@ -708,6 +602,14 @@ protected function get_known_failures(): array {
708602
'icu-regextst:1294',
709603
'icu-regextst:1295',
710604
'icu-regextst:1296',
605+
'icu-regextst:1470',
606+
'icu-regextst:1471',
607+
'icu-regextst:1472',
608+
'icu-regextst:1473',
609+
'icu-regextst:1478',
610+
'icu-regextst:1479',
611+
'icu-regextst:1480',
612+
'icu-regextst:1481',
711613
'icu-regextst:1504',
712614
'icu-regextst:1505'
713615
);

0 commit comments

Comments
 (0)