Skip to content

Commit fb8fe98

Browse files
committed
Fix undefined array key in WithStatement
The CTE name must be a valid identifier. Signed-off-by: Maurício Meneghini Fauth <[email protected]>
1 parent 8b3ab22 commit fb8fe98

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

src/Statements/WithStatement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use function array_slice;
1818
use function count;
19+
use function preg_match;
1920

2021
/**
2122
* `WITH` statement.
@@ -114,7 +115,7 @@ public function parse(Parser $parser, TokensList $list)
114115
}
115116

116117
if ($state === 0) {
117-
if ($token->type !== Token::TYPE_NONE) {
118+
if ($token->type !== Token::TYPE_NONE || ! preg_match('/^[a-zA-Z0-9_$]+$/', $token->token)) {
118119
$parser->error('The name of the CTE was expected.', $token);
119120
break;
120121
}

tests/Misc/BugsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function bugProvider(): array
2323
{
2424
return [
2525
['bugs/fuzz1'],
26+
['bugs/fuzz2'],
2627
['bugs/gh9'],
2728
['bugs/gh14'],
2829
['bugs/gh16'],

tests/data/bugs/fuzz2.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WITH](

tests/data/bugs/fuzz2.out

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"query": "WITH](",
3+
"lexer": {
4+
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
5+
"str": "WITH](",
6+
"len": 6,
7+
"last": 6,
8+
"list": {
9+
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
10+
"tokens": [
11+
{
12+
"@type": "PhpMyAdmin\\SqlParser\\Token",
13+
"token": "WITH",
14+
"value": "WITH",
15+
"keyword": "WITH",
16+
"type": 1,
17+
"flags": 3,
18+
"position": 0
19+
},
20+
{
21+
"@type": "PhpMyAdmin\\SqlParser\\Token",
22+
"token": "]",
23+
"value": "]",
24+
"keyword": null,
25+
"type": 0,
26+
"flags": 0,
27+
"position": 4
28+
},
29+
{
30+
"@type": "PhpMyAdmin\\SqlParser\\Token",
31+
"token": "(",
32+
"value": "(",
33+
"keyword": null,
34+
"type": 2,
35+
"flags": 16,
36+
"position": 5
37+
},
38+
{
39+
"@type": "PhpMyAdmin\\SqlParser\\Token",
40+
"token": null,
41+
"value": null,
42+
"keyword": null,
43+
"type": 9,
44+
"flags": 0,
45+
"position": null
46+
}
47+
],
48+
"count": 4,
49+
"idx": 4
50+
},
51+
"delimiter": ";",
52+
"delimiterLen": 1,
53+
"strict": false,
54+
"errors": []
55+
},
56+
"parser": {
57+
"@type": "PhpMyAdmin\\SqlParser\\Parser",
58+
"list": {
59+
"@type": "@1"
60+
},
61+
"statements": [
62+
{
63+
"@type": "PhpMyAdmin\\SqlParser\\Statements\\WithStatement",
64+
"withers": [],
65+
"cteStatementParser": null,
66+
"options": {
67+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray",
68+
"options": []
69+
},
70+
"first": 0,
71+
"last": 0
72+
}
73+
],
74+
"brackets": 1,
75+
"strict": false,
76+
"errors": []
77+
},
78+
"errors": {
79+
"lexer": [
80+
[
81+
"Unexpected character.",
82+
"]",
83+
4,
84+
0
85+
]
86+
],
87+
"parser": [
88+
[
89+
"The name of the CTE was expected.",
90+
{
91+
"@type": "@3"
92+
},
93+
0
94+
],
95+
[
96+
"Unexpected end of the WITH CTE.",
97+
{
98+
"@type": "@3"
99+
},
100+
0
101+
],
102+
[
103+
"Unexpected beginning of statement.",
104+
{
105+
"@type": "@3"
106+
},
107+
0
108+
]
109+
]
110+
}
111+
}

0 commit comments

Comments
 (0)