Repository navigation
Expand file tree
/
Copy pathWP_MySQL_Lexer_Tests.php
More file actions
686 lines (603 loc) · 28.9 KB
/
Copy pathWP_MySQL_Lexer_Tests.php
File metadata and controls
686 lines (603 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
<?php
use PHPUnit\Framework\TestCase;
/**
* Tests for the MySQL lexer: the token iterator API, the scanner behavior
* (identifiers, numbers, quoted strings), and the Bison token stream
* produced by WP_MySQL_Lexer::remaining_tokens().
*/
class WP_MySQL_Lexer_Tests extends TestCase {
public function test_tokenize_valid_input(): void {
$lexer = new WP_MySQL_Lexer( 'SELECT id FROM users' );
// SELECT
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['SELECT'], $lexer->get_token()->id );
// id
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::IDENTIFIER, $lexer->get_token()->id );
// FROM
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['FROM'], $lexer->get_token()->id );
// users
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::IDENTIFIER, $lexer->get_token()->id );
// The stream ends with END_OF_INPUT followed by Bison's end marker.
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::END_OF_INPUT, $lexer->get_token()->id );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::END_MARKER, $lexer->get_token()->id );
// No more tokens.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
// Again, no more tokens.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
}
public function test_tokenize_invalid_input(): void {
$lexer = new WP_MySQL_Lexer( "SELECT x'ab01xyz'" );
// SELECT
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['SELECT'], $lexer->get_token()->id );
// Invalid input.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
// No more tokens.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
// Again, no more tokens.
$this->assertFalse( $lexer->next_token() );
$this->assertNull( $lexer->get_token() );
}
/**
* Get the Bison terminal names of a tokenized input.
*
* @param string $sql The SQL payload to tokenize.
* @param string[] $sql_modes The SQL modes to activate.
* @return string[] Terminal names, in input order.
*/
private static function token_names( string $sql, array $sql_modes = array() ): array {
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, $sql_modes ) )->remaining_tokens();
$names = array();
foreach ( $tokens as $token ) {
$names[] = $token->get_name();
}
return $names;
}
public function test_emits_bison_terminals_with_end_markers(): void {
$this->assertSame(
array( 'SELECT', 'IDENTIFIER', 'FROM', 'IDENTIFIER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT id FROM users' )
);
}
public function test_ignore_space_does_not_absorb_whitespace_into_function_identifiers(): void {
// COUNT is a function keyword (SYM_FN). Under IGNORE_SPACE, "COUNT" that is
// not followed by "(" is a plain identifier, and its byte range must exclude
// the trailing whitespace that the mode skips while peeking for "(".
foreach ( array( 'SELECT COUNT FROM t', "SELECT COUNT\t\n FROM t" ) as $sql ) {
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
$this->assertSame( 'IDENTIFIER', $tokens[1]->get_name(), $sql );
$this->assertSame( 'COUNT', $tokens[1]->get_value(), $sql );
$this->assertSame( 5, $tokens[1]->length, $sql );
}
// When "(" does follow across whitespace, COUNT stays a function keyword and
// its byte range still excludes the whitespace.
$tokens = ( new WP_MySQL_Lexer( 'SELECT COUNT (1)', 80400, array( 'IGNORE_SPACE' ) ) )->remaining_tokens();
$this->assertSame( 5, $tokens[1]->length );
$this->assertNotSame( 'IDENTIFIER', $tokens[1]->get_name() );
$this->assertSame( 'OPEN_PAR_SYMBOL', $tokens[2]->get_name() );
}
public function test_version_comments_gate_the_body_by_version(): void {
// Five-digit version: the body is SQL only when the server version satisfies it.
$this->assertSame(
array( 'SELECT', 'INT_NUMBER', 'INT_NUMBER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT /*!50000 1 */ 2' )
);
$this->assertSame(
array( 'SELECT', 'INT_NUMBER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT /*!99999 1 */ 2' )
);
// Six-digit MMmmrr version (MySQL 8.4): a sixth digit followed by whitespace
// belongs to the version, not the body — so 080400 is consumed whole (no stray
// digit), and 100000 gates above 8.4.0 rather than as 10000.
$this->assertSame(
array( 'SELECT', 'INT_NUMBER', 'INT_NUMBER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT /*!080400 1 */ 2' )
);
$this->assertSame(
array( 'SELECT', 'INT_NUMBER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT /*!100000 1 */ 2' )
);
}
public function test_at_name_splits_into_at_and_ident(): void {
$tokens = ( new WP_MySQL_Lexer( 'SELECT @var1' ) )->remaining_tokens();
$this->assertSame( 'AT_SIGN_SYMBOL', $tokens[1]->get_name() );
$this->assertSame( 'IDENTIFIER', $tokens[2]->get_name() );
$this->assertSame( 'var1', $tokens[2]->get_value() );
$this->assertSame( 7, $tokens[1]->start );
$this->assertSame( 1, $tokens[1]->length );
$this->assertSame( 8, $tokens[2]->start );
$this->assertSame( 4, $tokens[2]->length );
}
public function test_at_at_splits_into_two_at_signs(): void {
$this->assertSame(
array( 'SELECT', 'AT_SIGN_SYMBOL', 'AT_SIGN_SYMBOL', 'IDENTIFIER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT @@sql_mode' )
);
}
public function test_bare_at_emits_empty_name(): void {
// MySQL's lexer emits an empty LEX_HOSTNAME after a bare "@", making
// "user1@" (an empty host part) and "SELECT @" valid.
$tokens = ( new WP_MySQL_Lexer( 'SELECT @' ) )->remaining_tokens();
$this->assertSame( 'AT_SIGN_SYMBOL', $tokens[1]->get_name() );
$this->assertSame( 'IDENTIFIER', $tokens[2]->get_name() );
$this->assertSame( 0, $tokens[2]->length );
$this->assertSame( '', $tokens[2]->get_value() );
$this->assertSame(
array( 'CREATE', 'USER', 'IDENTIFIER', 'AT_SIGN_SYMBOL', 'IDENTIFIER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'CREATE USER user1@' )
);
}
public function test_bare_at_before_quote_stands_alone(): void {
// In "@'name'" the quoted text supplies the name itself.
$this->assertSame(
array( 'SET', 'AT_SIGN_SYMBOL', 'SINGLE_QUOTED_TEXT', '=', 'INT_NUMBER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( "SET @'v' = 1" )
);
}
public function test_with_rollup_is_contracted(): void {
$names = self::token_names( 'SELECT 1 FROM t GROUP BY a WITH ROLLUP' );
$this->assertContains( 'WITH_ROLLUP_SYMBOL', $names );
$this->assertNotContains( 'WITH', $names );
}
public function test_with_rollup_contracts_across_comments(): void {
$tokens = ( new WP_MySQL_Lexer( 'SELECT 1 FROM t GROUP BY a WITH /* c */ ROLLUP' ) )->remaining_tokens();
$rollup = null;
foreach ( $tokens as $token ) {
if ( 'WITH_ROLLUP_SYMBOL' === $token->get_name() ) {
$rollup = $token;
}
}
$this->assertNotNull( $rollup );
$this->assertSame( 'WITH /* c */ ROLLUP', $rollup->get_bytes() );
}
public function test_lone_with_is_emitted(): void {
$this->assertSame(
array( 'WITH', 'IDENTIFIER', 'AS', 'OPEN_PAR_SYMBOL', 'SELECT', 'INT_NUMBER', 'CLOSE_PAR_SYMBOL', 'SELECT', 'MULT_OPERATOR', 'FROM', 'IDENTIFIER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'WITH c AS (SELECT 1) SELECT * FROM c' )
);
// A statement ending on WITH still emits it before the end markers.
$this->assertSame(
array( 'SELECT', 'INT_NUMBER', 'WITH', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT 1 WITH' )
);
}
public function test_invalid_input_returns_partial_stream_without_end_markers(): void {
$names = self::token_names( "SELECT 1 WITH \x01" );
$this->assertSame( array( 'SELECT', 'INT_NUMBER', 'WITH' ), $names );
}
public function test_high_not_precedence_emits_not2(): void {
$names = self::token_names( 'SELECT NOT 1', array( 'HIGH_NOT_PRECEDENCE' ) );
$this->assertContains( 'NOT2_SYMBOL', $names );
$names = self::token_names( 'SELECT NOT 1' );
$this->assertContains( 'NOT', $names );
}
public function test_end_of_input_word_is_an_identifier(): void {
// "end_of_input" is not a MySQL keyword; it must not truncate the stream.
$this->assertSame(
array( 'SELECT', 'IDENTIFIER', 'FROM', 'IDENTIFIER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT end_of_input FROM t' )
);
}
public function test_current_date_is_a_keyword_without_parentheses(): void {
// CURRENT_DATE/CURRENT_TIME are plain reserved keywords in MySQL 8.4
// (lex.h SYM), unlike CURDATE/CURTIME which require parentheses.
$this->assertSame(
array( 'SELECT', 'CURRENT_DATE', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT CURRENT_DATE' )
);
$this->assertSame(
array( 'SELECT', 'IDENTIFIER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT curdate' )
);
}
public function test_json_aggregates_are_keywords_only_before_parenthesis(): void {
$this->assertSame(
array( 'SELECT', 'IDENTIFIER', 'FROM', 'IDENTIFIER', 'END_OF_INPUT', 'END_MARKER' ),
self::token_names( 'SELECT json_objectagg FROM t' )
);
$names = self::token_names( 'SELECT JSON_OBJECTAGG(a, b) FROM t' );
$this->assertContains( 'JSON_OBJECTAGG', $names );
}
public function test_number_tokens_follow_mysql_magnitude_classes(): void {
$this->assertSame( array( 'SELECT', 'INT_NUMBER', 'END_OF_INPUT', 'END_MARKER' ), self::token_names( 'SELECT 2147483647' ) );
$this->assertSame( array( 'SELECT', 'LONG_NUMBER', 'END_OF_INPUT', 'END_MARKER' ), self::token_names( 'SELECT 2147483648' ) );
$this->assertSame( array( 'SELECT', 'ULONGLONG_NUMBER', 'END_OF_INPUT', 'END_MARKER' ), self::token_names( 'SELECT 18446744073709551615' ) );
$this->assertSame( array( 'SELECT', 'DECIMAL_NUMBER', 'END_OF_INPUT', 'END_MARKER' ), self::token_names( 'SELECT 18446744073709551616' ) );
}
/**
* Test that the whole U+0080 to U+FFFF UTF-8 range is valid in an identifier.
* The validity is checked against PCRE with the "u" (PCRE_UTF8) modifier set.
*/
public function test_identifier_utf8_range(): void {
for ( $i = 0x80; $i < 0xffff; $i += 1 ) {
$value = mb_chr( $i, 'UTF-8' );
$lexer = new WP_MySQL_Lexer( $value );
$this->assertTrue( $lexer->next_token() );
$type = $lexer->get_token()->id;
$is_valid = preg_match( '/^[\x{0080}-\x{ffff}]$/u', $value );
if ( $is_valid ) {
$this->assertSame( WP_MySQL_Lexer::IDENTIFIER, $type );
} else {
// A surrogate codepoint renders as an empty string, so the lexer
// emits no identifier — only the end-of-input terminal.
$this->assertSame( WP_MySQL_Lexer::END_OF_INPUT, $type );
}
}
}
/**
* Test all valid and invalid 2-byte UTF-8 sequences in an identifier.
* The validity is checked against PCRE with the "u" (PCRE_UTF8) modifier set.
*
* Start both bytes from 128 and go up to 255 to include all invalid 2-byte
* UTF-8 sequences as well, and ensure that they won't match as identifiers.
*/
public function test_identifier_utf8_two_byte_sequences(): void {
for ( $byte_1 = 128; $byte_1 <= 255; $byte_1 += 1 ) {
for ( $byte_2 = 128; $byte_2 <= 255; $byte_2 += 1 ) {
$value = chr( $byte_1 ) . chr( $byte_2 );
$lexer = new WP_MySQL_Lexer( $value );
$result = $lexer->next_token();
$token = $lexer->get_token();
$is_valid = preg_match( '/^[\x{0080}-\x{ffff}]$/u', $value );
if ( $is_valid ) {
$this->assertTrue( $result );
$this->assertSame( WP_MySQL_Lexer::IDENTIFIER, $token->id );
} else {
$this->assertFalse( $result );
$this->assertNull( $token );
}
}
}
}
/**
* Test all valid and invalid 3-byte UTF-8 sequences in an identifier.
* The validity is checked against PCRE with the "u" (PCRE_UTF8) modifier set.
*
* Start the first byte from 0xE0 to mark the beginning of a 3-byte sequence.
* Start bytes 2 and 3 from 128 and go up to 255 to include all invalid 3-byte
* UTF-8 sequences as well, and ensure that they won't match as identifiers.
*/
public function test_identifier_utf8_three_byte_sequences(): void {
for ( $byte_1 = 0xE0; $byte_1 <= 0xFF; $byte_1 += 1 ) {
for ( $byte_2 = 128; $byte_2 <= 255; $byte_2 += 1 ) {
for ( $byte_3 = 128; $byte_3 <= 255; $byte_3 += 1 ) {
$value = chr( $byte_1 ) . chr( $byte_2 ) . chr( $byte_3 );
$lexer = new WP_MySQL_Lexer( $value );
$result = $lexer->next_token();
$token = $lexer->get_token();
$is_valid = preg_match( '/^[\x{0080}-\x{ffff}]$/u', $value );
if ( $is_valid ) {
$this->assertTrue( $result );
$this->assertSame( WP_MySQL_Lexer::IDENTIFIER, $token->id );
} else {
$this->assertFalse( $result );
$this->assertNull( $token );
}
}
}
}
}
/**
* A charset-introducer-like name used as a qualified member (after a dot)
* must lex as an identifier. A real charset introducer only appears before
* a string literal, never as the member of a qualified reference.
*
* @dataProvider data_underscore_charset_after_dot
*/
public function test_underscore_charset_name_after_dot_is_identifier( string $sql, int $token_index, int $expected_id ): void {
$tokens = ( new WP_MySQL_Lexer( $sql ) )->remaining_tokens();
$this->assertSame(
WP_MySQL_Lexer::get_token_name( $expected_id ),
$tokens[ $token_index ]->get_name(),
$sql
);
}
/**
* @return array<string,array{0:string,1:int,2:int}>
*/
public function data_underscore_charset_after_dot(): array {
return array(
// `t . _utf8` - the member name must be an identifier, not a charset.
'charset name after dot is identifier' => array( 't._utf8', 2, WP_MySQL_Lexer::IDENTIFIER ),
'other charset name after dot' => array( 'a._binary', 2, WP_MySQL_Lexer::IDENTIFIER ),
// A genuine charset introducer (before a string) stays a charset.
'charset introducer before string' => array( "_utf8'x'", 0, WP_MySQL_Lexer::UNDERSCORE_CHARSET ),
// A non-charset underscore name after a dot stays an identifier.
'non-charset underscore name after dot' => array( 't._foo', 2, WP_MySQL_Lexer::IDENTIFIER ),
);
}
/**
* @dataProvider data_integer_types
*/
public function test_integer_types( $input, $expected ): void {
$lexer = new WP_MySQL_Lexer( $input );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( $expected, $lexer->get_token()->id );
}
public function data_integer_types(): array {
return array(
array( '0', WP_MySQL_Lexer::INT_NUMBER ),
array( '123', WP_MySQL_Lexer::INT_NUMBER ),
array( '2147483647', WP_MySQL_Lexer::INT_NUMBER ),
array( '00000000001', WP_MySQL_Lexer::INT_NUMBER ),
array( '00000000002147483647', WP_MySQL_Lexer::INT_NUMBER ),
array( '2147483648', WP_MySQL_Lexer::LONG_NUMBER ),
array( '123456789123456789', WP_MySQL_Lexer::LONG_NUMBER ),
array( '9223372036854775807', WP_MySQL_Lexer::LONG_NUMBER ),
array( '00000000002147483648', WP_MySQL_Lexer::LONG_NUMBER ),
array( '00000000009223372036854775807', WP_MySQL_Lexer::LONG_NUMBER ),
array( '9223372036854775808', WP_MySQL_Lexer::ULONGLONG_NUMBER ),
array( '12345678912345678912', WP_MySQL_Lexer::ULONGLONG_NUMBER ),
array( '18446744073709551615', WP_MySQL_Lexer::ULONGLONG_NUMBER ),
array( '00000000000000000009223372036854775808', WP_MySQL_Lexer::ULONGLONG_NUMBER ),
array( '000000000000000000018446744073709551615', WP_MySQL_Lexer::ULONGLONG_NUMBER ),
array( '18446744073709551616', WP_MySQL_Lexer::DECIMAL_NUMBER ),
array( '23456789123456789123', WP_MySQL_Lexer::DECIMAL_NUMBER ),
array( '123456789123456789123456789', WP_MySQL_Lexer::DECIMAL_NUMBER ),
array( '0000000000000000000018446744073709551616', WP_MySQL_Lexer::DECIMAL_NUMBER ),
array( '00000000000000000000123456789123456789123456789', WP_MySQL_Lexer::DECIMAL_NUMBER ),
);
}
/**
* Numbers vs. identifiers:
*
* In MySQL, when an input matches both a number and an identifier, the number always wins.
* However, when the number is followed by a non-numeric identifier-like character, it is
* considered an identifier... unless it's a float number, which ignores subsequent input.
*
* @dataProvider data_identifier_or_number
*/
public function test_identifier_or_number( $input, $expected ): void {
$lexer = new WP_MySQL_Lexer( $input );
$actual = array_map(
function ( $token ) {
return $token->id;
},
$lexer->remaining_tokens()
);
// Compare token names to get more readable error messages.
$this->assertSame(
$this->get_token_names( $expected ),
$this->get_token_names( $actual )
);
}
public function data_identifier_or_number(): array {
$end = array( WP_MySQL_Lexer::END_OF_INPUT, WP_MySQL_Lexer::END_MARKER );
return array(
// integer
array( '123', array_merge( array( WP_MySQL_Lexer::INT_NUMBER ), $end ) ),
array( '123abc', array_merge( array( WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // identifier
// binary
array( '0b01', array_merge( array( WP_MySQL_Lexer::BIN_NUMBER ), $end ) ),
array( '0b01xyz', array_merge( array( WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // identifier
array( '0b', array_merge( array( WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // identifier
array( "b'01'", array_merge( array( WP_MySQL_Lexer::BIN_NUMBER ), $end ) ),
array( "b'01xyz'", array() ), // invalid input
array( "b''", array_merge( array( WP_MySQL_Lexer::BIN_NUMBER ), $end ) ),
array( "b'", array() ), // invalid input
array( "b'01", array() ), // invalid input
// hex
array( '0xab01', array_merge( array( WP_MySQL_Lexer::HEX_NUMBER ), $end ) ),
array( '0xab01xyz', array_merge( array( WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // identifier
array( '0x', array_merge( array( WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // identifier
array( "x'ab01'", array_merge( array( WP_MySQL_Lexer::HEX_NUMBER ), $end ) ),
array( "x'ab01xyz'", array() ), // invalid input
array( "x''", array_merge( array( WP_MySQL_Lexer::HEX_NUMBER ), $end ) ),
array( "x'", array() ), // invalid input
array( "x'ab", array() ), // invalid input
// decimal
array( '123.456', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUMBER ), $end ) ),
array( '.123', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUMBER ), $end ) ),
array( '123.', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUMBER ), $end ) ),
array( '123.456abc', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '.123abc', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '123.abc', array_merge( array( WP_MySQL_Lexer::DECIMAL_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
// float
array( '1e10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '1e+10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '1e-10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '.1e10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '.1e+10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '.1e-10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '1.1e10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '1.1e-10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '1.1e+10', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER ), $end ) ),
array( '1e10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier (this differs from INT/BIN/HEX numbers)
array( '1e+10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '1e-10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '.1e10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '.1e+10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '.1e-10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '1.1e10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '1.1e+10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
array( '1.1e-10abc', array_merge( array( WP_MySQL_Lexer::FLOAT_NUMBER, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not identifier
// non-numbers
array( '.SELECT', array_merge( array( WP_MySQL_Lexer::DOT_SYMBOL, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not decimal or float
array( '1+e10', array_merge( array( WP_MySQL_Lexer::INT_NUMBER, WP_MySQL_Lexer::PLUS_OPERATOR, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not float
array( '1-e10', array_merge( array( WP_MySQL_Lexer::INT_NUMBER, WP_MySQL_Lexer::MINUS_OPERATOR, WP_MySQL_Lexer::IDENTIFIER ), $end ) ), // not float
);
}
/**
* Test that unclosed quoted strings with trailing backslashes do not
* cause out-of-bounds string access in read_quoted_text().
*
* The backslash-counting loop walks backward from the closing-quote
* candidate position. When the closing quote is missing and the last
* byte is a backslash, the loop must not treat the absent quote as
* escaped and advance past the end of the string, which would access
* an invalid string offset, triggering "Uninitialized string offset"
* warnings.
*
* @dataProvider data_unclosed_strings_with_backslashes
*/
public function test_unclosed_string_with_trailing_backslash( string $sql ): void {
set_error_handler(
function ( $severity, $message, $file, $line ) {
throw new \ErrorException( $message, 0, $severity, $file, $line );
},
E_WARNING | E_NOTICE
);
try {
$lexer = new WP_MySQL_Lexer( $sql );
while ( $lexer->next_token() ) {
// Consume all tokens.
}
} finally {
restore_error_handler();
}
// If we reach here without an ErrorException, no OOB access occurred.
$this->assertNull( $lexer->get_token() );
}
public function data_unclosed_strings_with_backslashes(): array {
return array(
'single-quoted trailing backslash' => array( "SELECT '\\" ),
'double-quoted trailing backslash' => array( 'SELECT "\\' ),
'even trailing backslashes' => array( "SELECT '\\\\" ),
'odd trailing backslashes' => array( "SELECT '\\\\\\" ),
'backslash-only single-quoted' => array( "'\\" ),
'backslash-only double-quoted' => array( '"\\' ),
);
}
/**
* Regression: valid strings with escapes must still tokenize correctly.
*
* @dataProvider data_valid_escaped_strings
*/
public function test_valid_escaped_string( string $sql, int $expected_token_id ): void {
$lexer = new WP_MySQL_Lexer( $sql );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( $expected_token_id, $lexer->get_token()->id );
}
public function data_valid_escaped_strings(): array {
return array(
'escaped single quote' => array( "'it\\'s'", WP_MySQL_Lexer::SINGLE_QUOTED_TEXT ),
'trailing escaped backslash' => array( "'path\\\\'", WP_MySQL_Lexer::SINGLE_QUOTED_TEXT ),
'doubled single quote' => array( "'it''s'", WP_MySQL_Lexer::SINGLE_QUOTED_TEXT ),
'empty single-quoted string' => array( "''", WP_MySQL_Lexer::SINGLE_QUOTED_TEXT ),
'escaped double quote' => array( '"col\\"name"', WP_MySQL_Lexer::DOUBLE_QUOTED_TEXT ),
'backtick identifier' => array( '`my_column`', WP_MySQL_Lexer::BACK_TICK_QUOTED_ID ),
);
}
/**
* Test that a chunk boundary splitting a quoted string with a trailing
* backslash does not cause an out-of-bounds string access.
*
* This simulates streaming SQL processing where a buffer boundary falls
* inside a string literal right after a backslash escape character.
*/
public function test_chunk_boundary_inside_escaped_string(): void {
set_error_handler(
function ( $severity, $message, $file, $line ) {
throw new \ErrorException( $message, 0, $severity, $file, $line );
},
E_WARNING | E_NOTICE
);
try {
// Build a SQL string where a backslash falls at the chunk boundary.
// The string content before the boundary is padded to place the
// backslash at exactly position $chunk_size - 1.
$chunk_size = 8192;
// "SELECT '" = 8 bytes, so we need chunk_size - 8 - 1 bytes of
// padding before the trailing backslash to place '\' at the last
// byte of the chunk.
$padding = str_repeat( 'A', $chunk_size - 8 - 1 );
$sql = "SELECT '" . $padding . '\\';
// The chunk is exactly $chunk_size bytes. The last byte is '\'.
// The lexer should handle this as an unclosed string without OOB.
$this->assertSame( $chunk_size, strlen( $sql ) );
$lexer = new WP_MySQL_Lexer( $sql );
while ( $lexer->next_token() ) {
// Consume all tokens.
}
} finally {
restore_error_handler();
}
$this->assertNull( $lexer->get_token() );
}
/**
* @dataProvider data_ansi_quotes_tokenization
*/
public function test_ansi_quotes_changes_double_quote_tokenization(
string $sql,
array $sql_modes,
string $expected_token_name
): void {
$tokens = ( new WP_MySQL_Lexer( $sql, 80400, $sql_modes ) )->remaining_tokens();
$this->assertSame( $expected_token_name, $tokens[0]->get_name(), $sql );
}
public function data_ansi_quotes_tokenization(): array {
return array(
'double quote is a string by default' => array( '"foo"', array(), 'SINGLE_QUOTED_TEXT' ),
'double quote is an identifier in ANSI_QUOTES' => array( '"foo"', array( 'ANSI_QUOTES' ), 'BACK_TICK_QUOTED_ID' ),
'ANSI_QUOTES is case-insensitive' => array( '"foo"', array( 'ansi_quotes' ), 'BACK_TICK_QUOTED_ID' ),
'ANSI_QUOTES with other modes' => array( '"foo"', array( 'STRICT_ALL_TABLES', 'ANSI_QUOTES' ), 'BACK_TICK_QUOTED_ID' ),
'single quote stays a string in ANSI_QUOTES' => array( "'foo'", array( 'ANSI_QUOTES' ), 'SINGLE_QUOTED_TEXT' ),
'backtick stays an identifier in ANSI_QUOTES' => array( '`foo`', array( 'ANSI_QUOTES' ), 'BACK_TICK_QUOTED_ID' ),
);
}
public function test_composite_ansi_mode_expands_to_lexer_component_modes(): void {
// ANSI_QUOTES: a double-quoted sequence is a quoted identifier.
$lexer = new WP_MySQL_Lexer( '"foo"', 80400, array( 'ANSI' ) );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, $lexer->get_token()->id );
// PIPES_AS_CONCAT: "||" is the string concatenation operator.
$lexer = new WP_MySQL_Lexer( '||', 80400, array( 'ANSI' ) );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::CONCAT_PIPES_SYMBOL, $lexer->get_token()->id );
// IGNORE_SPACE: whitespace is permitted between a function name and "(".
$lexer = new WP_MySQL_Lexer( 'COUNT (1)', 80400, array( 'ANSI' ) );
$this->assertTrue( $lexer->next_token() );
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['COUNT'], $lexer->get_token()->id );
}
/**
* Quoted identifiers escape delimiters by doubling, not with backslashes.
*
* @dataProvider data_quoted_identifier_backslash
*/
public function test_quoted_identifiers_treat_backslash_literally(
string $sql,
array $sql_modes,
int $expected_token_id,
string $expected_value
): void {
$lexer = new WP_MySQL_Lexer( $sql, 80400, $sql_modes );
$this->assertTrue( $lexer->next_token() );
$token = $lexer->get_token();
$this->assertSame( $expected_token_id, $token->id, $sql );
$this->assertSame( $expected_value, $token->get_value(), $sql );
}
public function data_quoted_identifier_backslash(): array {
$bs = chr( 92 ); // A single backslash.
$bt = '`';
$dq = '"';
$sq = "'";
return array(
// Backtick identifiers: backslash is a literal character.
'backtick keeps backslash-n literal' => array( $bt . 'a' . $bs . 'nb' . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs . 'nb' ),
'backtick allows trailing backslash' => array( $bt . 'a' . $bs . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs ),
'backtick escapes only by doubling' => array( $bt . 'a' . $bt . $bt . 'b' . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bt . 'b' ),
// ANSI-quoted identifiers behave like backtick identifiers.
'ansi-quoted keeps backslash literal' => array( $dq . 'a' . $bs . 'nb' . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs . 'nb' ),
'ansi-quoted allows trailing backslash' => array( $dq . 'a' . $bs . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs ),
// String literals still process backslash escapes.
'single-quoted string escapes newline' => array( $sq . 'a' . $bs . 'nb' . $sq, array(), WP_MySQL_Lexer::SINGLE_QUOTED_TEXT, "a\nb" ),
);
}
private function get_token_names( array $token_types ): array {
return array_map(
function ( $token_type ) {
return WP_MySQL_Lexer::get_token_name( $token_type );
},
$token_types
);
}
}