From a120958b0601412df7cf1becb9f457e2ce96d51f Mon Sep 17 00:00:00 2001 From: kanywst Date: Thu, 18 Jun 2026 01:39:45 +0900 Subject: [PATCH] fix(lexer): prevent panic on unterminated string literal --- pkg/dsl/lexer/lexer.go | 8 +++++++- pkg/dsl/lexer/lexer_test.go | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/dsl/lexer/lexer.go b/pkg/dsl/lexer/lexer.go index 5a8b38fe4..eb4e0de5b 100644 --- a/pkg/dsl/lexer/lexer.go +++ b/pkg/dsl/lexer/lexer.go @@ -228,7 +228,13 @@ func (l *Lexer) lexString() string { } l.readChar() } - str += l.input[position:l.position] + end := l.position + if end > len(l.input) { + end = len(l.input) + } + if position < end { + str += l.input[position:end] + } if l.ch == '"' { l.readChar() } diff --git a/pkg/dsl/lexer/lexer_test.go b/pkg/dsl/lexer/lexer_test.go index 969054738..1ee6f7ef2 100644 --- a/pkg/dsl/lexer/lexer_test.go +++ b/pkg/dsl/lexer/lexer_test.go @@ -1189,4 +1189,17 @@ rule test_rule(created_at time, started_at time) { Expect(hasBoolean).Should(BeTrue()) Expect(hasComparison).Should(BeTrue()) }) + + It("Case 4 - should not panic on an unterminated string", func() { + for _, input := range []string{`"\`, `"`, `"abc\`, `"\\`} { + Expect(func() { + l := NewLexer(input) + for { + if l.NextToken().Type == token.EOF { + break + } + } + }).ShouldNot(Panic()) + } + }) })