Skip to content

Fix infinite loop (DoS hang) on unterminated block comment#271

Merged
git-hulk merged 1 commit into
masterfrom
fix/lexer-unterminated-comment-hang
Jun 10, 2026
Merged

Fix infinite loop (DoS hang) on unterminated block comment#271
git-hulk merged 1 commit into
masterfrom
fix/lexer-unterminated-comment-hang

Conversation

@git-hulk

Copy link
Copy Markdown
Member

Problem

consumeMultiLineComment loops on !l.isEOF(), which tests l.current, but only advances a local index i. When a block comment is never closed, l.current never moves, so isEOF() never becomes true and the lexer spins forever — a denial-of-service hang on malformed input.

Reproduction

parser.NewParser("/* unterminated").ParseStmts() // hangs forever
parser.NewParser("SELECT 1 /* x").ParseStmts()   // hangs forever

(The exact 2-byte input /* is safe because skipN(2) already leaves current at EOF, but any content after the opener triggers the hang.)

Fix

Loop on l.peekOk(i) instead, so the loop tracks the local index and terminates at end of input. When no closing */ is found, the remainder is consumed as comment — matching consumeSingleLineComment's behaviour.

Test

TestConsumeUnterminatedComment drains the lexer in a goroutine guarded by a timeout, so a regression fails fast instead of hanging the test binary.

🤖 Generated with Claude Code

`consumeMultiLineComment` looped on `!l.isEOF()` (which checks `l.current`)
while only advancing the local index `i`, so on an unclosed comment like
`/* unterminated` the lexer spun forever — a DoS hang.

Loop on `l.peekOk(i)` instead so it terminates at end of input.

Add TestConsumeUnterminatedComment to reproduce: it drains the lexer in a
goroutine with a timeout, so a regression fails fast instead of hanging.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@git-hulk git-hulk requested a review from Lance726 June 10, 2026 06:44
@git-hulk git-hulk merged commit 4d7ccd6 into master Jun 10, 2026
4 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