From 796b135d10ab384e9e2d7e5b276ac08499d9ccec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 15 Sep 2023 15:00:08 -0300 Subject: [PATCH] Fix invalid hexadecimal prefix 0X MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fixes #508 Signed-off-by: MaurĂ­cio Meneghini Fauth --- src/Lexer.php | 5 +- tests/Misc/BugsTest.php | 1 + tests/data/bugs/gh508.in | 1 + tests/data/bugs/gh508.out | 60 +++++++++++ tests/data/lexer/lexNumber.in | 6 +- tests/data/lexer/lexNumber.out | 185 +++++++++++++++++---------------- 6 files changed, 163 insertions(+), 95 deletions(-) create mode 100644 tests/data/bugs/gh508.in create mode 100644 tests/data/bugs/gh508.out diff --git a/src/Lexer.php b/src/Lexer.php index befaf4973..73b5aa060 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -851,10 +851,7 @@ public function parseNumber() } elseif ( $this->last + 1 < $this->len && $this->str[$this->last] === '0' - && ( - $this->str[$this->last + 1] === 'x' - || $this->str[$this->last + 1] === 'X' - ) + && $this->str[$this->last + 1] === 'x' ) { $token .= $this->str[$this->last++]; $state = 2; diff --git a/tests/Misc/BugsTest.php b/tests/Misc/BugsTest.php index f457f4cd3..f012caccb 100644 --- a/tests/Misc/BugsTest.php +++ b/tests/Misc/BugsTest.php @@ -26,6 +26,7 @@ public function bugProvider(): array ['bugs/gh14'], ['bugs/gh16'], ['bugs/gh317'], + ['bugs/gh508'], ['bugs/pma11800'], ['bugs/pma11836'], ['bugs/pma11843'], diff --git a/tests/data/bugs/gh508.in b/tests/data/bugs/gh508.in new file mode 100644 index 000000000..bef3a359a --- /dev/null +++ b/tests/data/bugs/gh508.in @@ -0,0 +1 @@ +0X0F \ No newline at end of file diff --git a/tests/data/bugs/gh508.out b/tests/data/bugs/gh508.out new file mode 100644 index 000000000..085f75e6d --- /dev/null +++ b/tests/data/bugs/gh508.out @@ -0,0 +1,60 @@ +{ + "query": "0X0F", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "str": "0X0F", + "len": 4, + "last": 4, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "0X0F", + "value": "0X0F", + "keyword": null, + "type": 0, + "flags": 0, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": null, + "value": null, + "keyword": null, + "type": 9, + "flags": 0, + "position": null + } + ], + "count": 2, + "idx": 2 + }, + "delimiter": ";", + "delimiterLen": 1, + "strict": false, + "errors": [] + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "list": { + "@type": "@1" + }, + "statements": [], + "brackets": 0, + "strict": false, + "errors": [] + }, + "errors": { + "lexer": [], + "parser": [ + [ + "Unexpected beginning of statement.", + { + "@type": "@2" + }, + 0 + ] + ] + } +} \ No newline at end of file diff --git a/tests/data/lexer/lexNumber.in b/tests/data/lexer/lexNumber.in index 6fe38851d..5751b0987 100644 --- a/tests/data/lexer/lexNumber.in +++ b/tests/data/lexer/lexNumber.in @@ -1,3 +1,3 @@ -SELECT 12, 34, 5.67, 0x89, -10, --11, +12, .15, 0xFFa, 0xfFA, 0XFfA, -0xFFa, -0xfFA, -0XFfA, 1e-10, 1e10, .5e10, b'10'; --- invalid number -SELECT 12ex10, b'15'; \ No newline at end of file +SELECT 12, 34, 5.67, 0x89, -10, --11, +12, .15, 0xFFa, 0xfFA, -0xFFa, -0xfFA, 1e-10, 1e10, .5e10, b'10'; +-- invalid numbers +SELECT 12ex10, b'15', 0XFfA, -0XFfA; \ No newline at end of file diff --git a/tests/data/lexer/lexNumber.out b/tests/data/lexer/lexNumber.out index f28612d86..2609bb428 100644 --- a/tests/data/lexer/lexNumber.out +++ b/tests/data/lexer/lexNumber.out @@ -1,10 +1,10 @@ { - "query": "SELECT 12, 34, 5.67, 0x89, -10, --11, +12, .15, 0xFFa, 0xfFA, 0XFfA, -0xFFa, -0xfFA, -0XFfA, 1e-10, 1e10, .5e10, b'10';\n-- invalid number\nSELECT 12ex10, b'15';", + "query": "SELECT 12, 34, 5.67, 0x89, -10, --11, +12, .15, 0xFFa, 0xfFA, -0xFFa, -0xfFA, 1e-10, 1e10, .5e10, b'10';\n-- invalid numbers\nSELECT 12ex10, b'15', 0XFfA, -0XFfA;", "lexer": { "@type": "PhpMyAdmin\\SqlParser\\Lexer", - "str": "SELECT 12, 34, 5.67, 0x89, -10, --11, +12, .15, 0xFFa, 0xfFA, 0XFfA, -0xFFa, -0xfFA, -0XFfA, 1e-10, 1e10, .5e10, b'10';\n-- invalid number\nSELECT 12ex10, b'15';", - "len": 159, - "last": 159, + "str": "SELECT 12, 34, 5.67, 0x89, -10, --11, +12, .15, 0xFFa, 0xfFA, -0xFFa, -0xfFA, 1e-10, 1e10, .5e10, b'10';\n-- invalid numbers\nSELECT 12ex10, b'15', 0XFfA, -0XFfA;", + "len": 160, + "last": 160, "list": { "@type": "PhpMyAdmin\\SqlParser\\TokensList", "tokens": [ @@ -296,33 +296,6 @@ "flags": 0, "position": 61 }, - { - "@type": "PhpMyAdmin\\SqlParser\\Token", - "token": "0XFfA", - "value": 4090, - "keyword": null, - "type": 6, - "flags": 1, - "position": 62 - }, - { - "@type": "PhpMyAdmin\\SqlParser\\Token", - "token": ",", - "value": ",", - "keyword": null, - "type": 2, - "flags": 16, - "position": 67 - }, - { - "@type": "PhpMyAdmin\\SqlParser\\Token", - "token": " ", - "value": " ", - "keyword": null, - "type": 3, - "flags": 0, - "position": 68 - }, { "@type": "PhpMyAdmin\\SqlParser\\Token", "token": "-0xFFa", @@ -330,7 +303,7 @@ "keyword": null, "type": 6, "flags": 9, - "position": 69 + "position": 62 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -339,7 +312,7 @@ "keyword": null, "type": 2, "flags": 16, - "position": 75 + "position": 68 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -348,7 +321,7 @@ "keyword": null, "type": 3, "flags": 0, - "position": 76 + "position": 69 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -357,7 +330,7 @@ "keyword": null, "type": 6, "flags": 9, - "position": 77 + "position": 70 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -366,34 +339,7 @@ "keyword": null, "type": 2, "flags": 16, - "position": 83 - }, - { - "@type": "PhpMyAdmin\\SqlParser\\Token", - "token": " ", - "value": " ", - "keyword": null, - "type": 3, - "flags": 0, - "position": 84 - }, - { - "@type": "PhpMyAdmin\\SqlParser\\Token", - "token": "-0XFfA", - "value": -4090, - "keyword": null, - "type": 6, - "flags": 9, - "position": 85 - }, - { - "@type": "PhpMyAdmin\\SqlParser\\Token", - "token": ",", - "value": ",", - "keyword": null, - "type": 2, - "flags": 16, - "position": 91 + "position": 76 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -402,7 +348,7 @@ "keyword": null, "type": 3, "flags": 0, - "position": 92 + "position": 77 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -411,7 +357,7 @@ "keyword": null, "type": 6, "flags": 4, - "position": 93 + "position": 78 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -420,7 +366,7 @@ "keyword": null, "type": 2, "flags": 16, - "position": 98 + "position": 83 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -429,7 +375,7 @@ "keyword": null, "type": 3, "flags": 0, - "position": 99 + "position": 84 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -438,7 +384,7 @@ "keyword": null, "type": 6, "flags": 4, - "position": 100 + "position": 85 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -447,7 +393,7 @@ "keyword": null, "type": 2, "flags": 16, - "position": 104 + "position": 89 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -456,7 +402,7 @@ "keyword": null, "type": 3, "flags": 0, - "position": 105 + "position": 90 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -465,7 +411,7 @@ "keyword": null, "type": 6, "flags": 6, - "position": 106 + "position": 91 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -474,7 +420,7 @@ "keyword": null, "type": 2, "flags": 16, - "position": 111 + "position": 96 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -483,7 +429,7 @@ "keyword": null, "type": 3, "flags": 0, - "position": 112 + "position": 97 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -492,7 +438,7 @@ "keyword": null, "type": 6, "flags": 16, - "position": 113 + "position": 98 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -501,7 +447,7 @@ "keyword": null, "type": 9, "flags": 0, - "position": 118 + "position": 103 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -510,16 +456,16 @@ "keyword": null, "type": 3, "flags": 0, - "position": 119 + "position": 104 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", - "token": "-- invalid number", - "value": "-- invalid number", + "token": "-- invalid numbers", + "value": "-- invalid numbers", "keyword": null, "type": 4, "flags": 4, - "position": 120 + "position": 105 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -528,7 +474,7 @@ "keyword": null, "type": 3, "flags": 0, - "position": 137 + "position": 123 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -537,7 +483,7 @@ "keyword": "SELECT", "type": 1, "flags": 3, - "position": 138 + "position": 124 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -546,7 +492,7 @@ "keyword": null, "type": 3, "flags": 0, - "position": 144 + "position": 130 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -555,7 +501,7 @@ "keyword": null, "type": 0, "flags": 0, - "position": 145 + "position": 131 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -564,7 +510,7 @@ "keyword": null, "type": 2, "flags": 16, - "position": 151 + "position": 137 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -573,7 +519,7 @@ "keyword": null, "type": 3, "flags": 0, - "position": 152 + "position": 138 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -582,7 +528,7 @@ "keyword": null, "type": 0, "flags": 0, - "position": 153 + "position": 139 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -591,6 +537,69 @@ "keyword": null, "type": 7, "flags": 1, + "position": 140 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ",", + "value": ",", + "keyword": null, + "type": 2, + "flags": 16, + "position": 144 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 145 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "0XFfA", + "value": "0XFfA", + "keyword": null, + "type": 0, + "flags": 0, + "position": 146 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": ",", + "value": ",", + "keyword": null, + "type": 2, + "flags": 16, + "position": 151 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": 3, + "flags": 0, + "position": 152 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "-", + "value": "-", + "keyword": null, + "type": 2, + "flags": 1, + "position": 153 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "0XFfA", + "value": "0XFfA", + "keyword": null, + "type": 0, + "flags": 0, "position": 154 }, { @@ -600,7 +609,7 @@ "keyword": null, "type": 9, "flags": 0, - "position": 158 + "position": 159 }, { "@type": "PhpMyAdmin\\SqlParser\\Token", @@ -612,7 +621,7 @@ "position": null } ], - "count": 67, + "count": 68, "idx": 0 }, "delimiter": ";",