From 08a3dd0da6c1c73e793f545bb0b3bb3665dc44be Mon Sep 17 00:00:00 2001 From: ManthanNimodiya Date: Fri, 19 Jun 2026 06:34:55 +0530 Subject: [PATCH] fix(dsl): prevent lexer panic on string ending in trailing backslash --- pkg/dsl/lexer/lexer.go | 6 ++++++ pkg/dsl/lexer/lexer_test.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/dsl/lexer/lexer.go b/pkg/dsl/lexer/lexer.go index 5a8b38fe4..c9927709e 100644 --- a/pkg/dsl/lexer/lexer.go +++ b/pkg/dsl/lexer/lexer.go @@ -212,6 +212,12 @@ func (l *Lexer) lexString() string { if l.ch == '\\' { str += l.input[position:l.position] l.readChar() // Skip the backslash + if l.ch == 0 { + // Unterminated string: input ended right after a trailing + // backslash, so there's no character left to escape. + position = l.position + break + } switch l.ch { case 'n': str += "\n" diff --git a/pkg/dsl/lexer/lexer_test.go b/pkg/dsl/lexer/lexer_test.go index 969054738..eef2f2212 100644 --- a/pkg/dsl/lexer/lexer_test.go +++ b/pkg/dsl/lexer/lexer_test.go @@ -1189,4 +1189,20 @@ rule test_rule(created_at time, started_at time) { Expect(hasBoolean).Should(BeTrue()) Expect(hasComparison).Should(BeTrue()) }) + + It("Case 11 - Unterminated string ending in a trailing backslash should not panic", func() { + // Regression test: a string literal that ends right after a backslash + // (no character left to escape) used to overshoot the input bounds + // and panic with a slice-out-of-range error. + l := NewLexer("\"\\") + + Expect(func() { + for { + tok := l.NextToken() + if tok.Type == token.EOF { + break + } + } + }).ShouldNot(Panic()) + }) })