Skip to content

Commit 3bdf97f

Browse files
git-hulkclaude
andcommitted
Fix index-out-of-range panic in wrapError at EOF
`wrapError` walks `p.lexer.input[i]` for `i < int(p.Pos())` to compute the error line/column. At EOF `p.Pos()` can exceed `len(input)`, so on input like `CREATE--` (a trailing comment leaves the position past the end) it panics with "index out of range". Clamp the loop bound to `len(p.lexer.input)`. Add `CREATE--` to TestParser_InvalidSyntax to reproduce: it panicked before the fix and now returns a proper error. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 88f462c commit 3bdf97f

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

parser/parser_common.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,13 @@ func (p *Parser) wrapError(err error) error {
374374
lineNo := 0
375375
column := 0
376376

377-
for i := 0; i < int(p.Pos()); i++ {
377+
// p.Pos() can exceed the input length when the lexer is at EOF,
378+
// so clamp the upper bound to avoid an index-out-of-range panic.
379+
upperBound := int(p.Pos())
380+
if upperBound > len(p.lexer.input) {
381+
upperBound = len(p.lexer.input)
382+
}
383+
for i := 0; i < upperBound; i++ {
378384
if p.lexer.input[i] == '\n' {
379385
lineNo++
380386
column = 0

parser/parser_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ func TestParser_InvalidSyntax(t *testing.T) {
186186
// Invalid ARRAY JOIN types (only ARRAY JOIN, LEFT ARRAY JOIN, and INNER ARRAY JOIN are valid)
187187
"SELECT * FROM t RIGHT ARRAY JOIN arr AS a", // RIGHT ARRAY JOIN not supported
188188
"SELECT * FROM t FULL ARRAY JOIN arr AS a", // FULL ARRAY JOIN not supported
189-
"00e1d", // invalid number that leaves lastToken nil
189+
"00e1d", // invalid number that leaves lastToken nil
190+
"CREATE--", // trailing comment pushes p.Pos() past end of input (wrapError out-of-range)
190191
}
191192
for _, sql := range invalidSQLs {
192193
parser := NewParser(sql)

0 commit comments

Comments
 (0)