Skip to content

Commit 445804d

Browse files
Merge pull request #510 from MauricioFauth/fuzz-errors
Fix undefined array key errors and a deprecation
2 parents 3d0c3a6 + dd1e775 commit 445804d

23 files changed

+708
-58
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ parameters:
1515
count: 1
1616
path: src/Components/AlterOperation.php
1717

18-
-
19-
message: "#^Parameter \\#1 \\$key of function array_key_exists expects int\\|string, float\\|int\\|string given\\.$#"
20-
count: 2
21-
path: src/Components/AlterOperation.php
22-
2318
-
2419
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\AlterOperation\\:\\:\\$options \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
2520
count: 1

psalm-baseline.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
<InvalidScalarArgument occurrences="1">
1313
<code>$arrayKey</code>
1414
</InvalidScalarArgument>
15-
<MixedArrayOffset occurrences="1">
16-
<code>Parser::$STATEMENT_PARSERS[$token-&gt;value]</code>
17-
</MixedArrayOffset>
1815
<MoreSpecificImplementedParamType occurrences="1">
1916
<code>$component</code>
2017
</MoreSpecificImplementedParamType>
@@ -631,8 +628,7 @@
631628
<code>$this-&gt;last</code>
632629
<code>$this-&gt;last</code>
633630
</LoopInvalidation>
634-
<MixedArrayAccess occurrences="42">
635-
<code>$this-&gt;str[$this-&gt;last + 1]</code>
631+
<MixedArrayAccess occurrences="41">
636632
<code>$this-&gt;str[$this-&gt;last + 1]</code>
637633
<code>$this-&gt;str[$this-&gt;last++]</code>
638634
<code>$this-&gt;str[$this-&gt;last]</code>

src/Components/AlterOperation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use function array_key_exists;
1313
use function in_array;
14-
use function is_numeric;
14+
use function is_int;
1515
use function is_string;
1616
use function trim;
1717

@@ -412,7 +412,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
412412

413413
$state = 2;
414414
} elseif ($state === 2) {
415-
if (is_string($token->value) || is_numeric($token->value)) {
415+
if (is_string($token->value) || is_int($token->value)) {
416416
$arrayKey = $token->value;
417417
} else {
418418
$arrayKey = $token->token;
@@ -445,7 +445,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
445445
);
446446
break;
447447
}
448-
} elseif (! empty(Parser::$STATEMENT_PARSERS[$token->value])) {
448+
} elseif (! empty(Parser::$STATEMENT_PARSERS[$arrayKey])) {
449449
// We have reached the end of ALTER operation and suddenly found
450450
// a start to new statement, but have not found a delimiter between them
451451
$parser->error(

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/AlterStatement.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ class AlterStatement extends Statement
6767
public function parse(Parser $parser, TokensList $list)
6868
{
6969
++$list->idx; // Skipping `ALTER`.
70-
$this->options = OptionsArray::parse($parser, $list, static::$OPTIONS);
70+
$parsedOptions = OptionsArray::parse($parser, $list, static::$OPTIONS);
71+
if ($parsedOptions->isEmpty()) {
72+
$parser->error('Unrecognized alter operation.', $list->tokens[$list->idx]);
73+
74+
return;
75+
}
76+
77+
$this->options = $parsedOptions;
7178
++$list->idx;
7279

7380
// Parsing affected table.

src/Statements/WithStatement.php

Lines changed: 8 additions & 2 deletions
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
}
@@ -124,7 +125,12 @@ public function parse(Parser $parser, TokensList $list)
124125
$state = 1;
125126
} elseif ($state === 1) {
126127
if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') {
127-
$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;
128134
$state = 2;
129135
} elseif ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'AS') {
130136
$state = 3;

src/Token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ public function extract()
254254
case self::TYPE_NUMBER:
255255
$ret = str_replace('--', '', $this->token); // e.g. ---42 === -42
256256
if ($this->flags & self::FLAG_NUMBER_HEX) {
257+
$ret = str_replace(['-', '+'], '', $this->token);
257258
if ($this->flags & self::FLAG_NUMBER_NEGATIVE) {
258-
$ret = str_replace('-', '', $this->token);
259259
$ret = -hexdec($ret);
260260
} else {
261261
$ret = hexdec($ret);

tests/Misc/BugsTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function testBug(string $test): void
2222
public function bugProvider(): array
2323
{
2424
return [
25+
['bugs/fuzz1'],
26+
['bugs/fuzz2'],
27+
['bugs/fuzz3'],
28+
['bugs/fuzz4'],
2529
['bugs/gh9'],
2630
['bugs/gh14'],
2731
['bugs/gh16'],

tests/data/bugs/fuzz1.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER..2

tests/data/bugs/fuzz1.out

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"query": "ALTER..2",
3+
"lexer": {
4+
"@type": "PhpMyAdmin\\SqlParser\\Lexer",
5+
"str": "ALTER..2",
6+
"len": 8,
7+
"last": 8,
8+
"list": {
9+
"@type": "PhpMyAdmin\\SqlParser\\TokensList",
10+
"tokens": [
11+
{
12+
"@type": "PhpMyAdmin\\SqlParser\\Token",
13+
"token": "ALTER",
14+
"value": "ALTER",
15+
"keyword": "ALTER",
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": 2,
26+
"flags": 16,
27+
"position": 5
28+
},
29+
{
30+
"@type": "PhpMyAdmin\\SqlParser\\Token",
31+
"token": ".2",
32+
"value": 0.2,
33+
"keyword": null,
34+
"type": 6,
35+
"flags": 2,
36+
"position": 6
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\\AlterStatement",
64+
"table": null,
65+
"altered": [],
66+
"options": null,
67+
"first": 0,
68+
"last": 0
69+
}
70+
],
71+
"brackets": 0,
72+
"strict": false,
73+
"errors": []
74+
},
75+
"errors": {
76+
"lexer": [],
77+
"parser": [
78+
[
79+
"Unrecognized alter operation.",
80+
{
81+
"@type": "@2"
82+
},
83+
0
84+
],
85+
[
86+
"Unexpected beginning of statement.",
87+
{
88+
"@type": "@4"
89+
},
90+
0
91+
]
92+
]
93+
}
94+
}

0 commit comments

Comments
 (0)