Skip to content

Fix nil-pointer panic when ORDER BY ends with AS - #340

Merged
git-hulk merged 1 commit into
AfterShip:masterfrom
laypatel13:fix-order-by-as-at-eof
Nov 12, 2026
Merged

git-hulk merged 1 commit into
AfterShip:masterfrom
laypatel13:fix-order-by-as-at-eof

Conversation

@laypatel13

Copy link
Copy Markdown
Contributor

Problem

parseOrderExpr checks whether the word after AS is SELECT. When AS is the last word of the input, peekToken() returns a nil token with no error, so nextToken.ToString() panics.

I found this through Jaeger, which uses this parser for its SQL query tools. A query ending in ORDER BY duration AS crashed the handler instead of returning an error jaegertracing/jaeger#10134

Reproduction

parser.NewParser("SELECT * FROM t ORDER BY a AS").ParseStmts()
// panic: runtime error: invalid memory address or nil pointer dereference

Fix

Check nextToken != nil before calling ToString(). With nothing after AS, parsing continues to parseAnyKeyword(), which returns a normal error:

line 1:30 expected '<ident>', but got '<eof>'

Test

Added SELECT * FROM t ORDER BY a AS to TestParser_InvalidSyntax. It panicked before the fix and returns an error after it. make test passes.

Signed-off-by: Lay Patel <lay.patel.1313@gmail.com>
@git-hulk

Copy link
Copy Markdown
Member

@laypatel13 Good catch! Thank you.

@git-hulk
git-hulk merged commit fddb0c9 into AfterShip:master Nov 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants