Skip to content

Commit b91a3a9

Browse files
committed
Fix undefined array key in WithStatement
Check if CTE column brackets was closed. Signed-off-by: Maurício Meneghini Fauth <[email protected]>
1 parent fb8fe98 commit b91a3a9

File tree

6 files changed

+134
-2
lines changed

6 files changed

+134
-2
lines changed

src/Components/ArrayObj.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public static function parse(Parser $parser, TokensList $list, array $options =
9292

9393
// End of statement.
9494
if ($token->type === Token::TYPE_DELIMITER) {
95+
if ($brackets > 0) {
96+
$parser->error('A closing bracket was expected.', $token);
97+
}
98+
9599
break;
96100
}
97101

src/Statements/WithStatement.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ public function parse(Parser $parser, TokensList $list)
125125
$state = 1;
126126
} elseif ($state === 1) {
127127
if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') {
128-
$this->withers[$wither]->columns = Array2d::parse($parser, $list);
128+
$columns = Array2d::parse($parser, $list);
129+
if ($parser->errors !== []) {
130+
break;
131+
}
132+
133+
$this->withers[$wither]->columns = $columns;
129134
$state = 2;
130135
} elseif ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'AS') {
131136
$state = 3;

tests/Misc/BugsTest.php

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

tests/data/bugs/fuzz3.in

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

tests/data/bugs/fuzz3.out

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

tests/data/parser/parseArrayErr3.out

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@
239239
},
240240
"errors": {
241241
"lexer": [],
242-
"parser": []
242+
"parser": [
243+
[
244+
"A closing bracket was expected.",
245+
{
246+
"@type": "@17"
247+
},
248+
0
249+
]
250+
]
243251
}
244252
}

0 commit comments

Comments
 (0)