Skip to content

Commit 5df9a6c

Browse files
git-hulkclaudeCopilot
authored
Fix index-out-of-range panic in wrapError at EOF (#272)
`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> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 4d7ccd6 commit 5df9a6c

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
@@ -384,7 +384,13 @@ func (p *Parser) wrapError(err error) error {
384384
lineNo := 0
385385
column := 0
386386

387-
for i := 0; i < int(p.Pos()); i++ {
387+
// p.Pos() can exceed the input length when the lexer is at EOF,
388+
// so clamp the upper bound to avoid an index-out-of-range panic.
389+
upperBound := int(p.Pos())
390+
if upperBound > len(p.lexer.input) {
391+
upperBound = len(p.lexer.input)
392+
}
393+
for i := 0; i < upperBound; i++ {
388394
if p.lexer.input[i] == '\n' {
389395
lineNo++
390396
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
// Inputs that previously caused a nil-pointer dereference while
191192
// formatting the error message at EOF (p.last() is nil).
192193
"ALTER ",

0 commit comments

Comments
 (0)