Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/dsl/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions pkg/dsl/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Comment on lines +1193 to +1207

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Search for existing tests covering escape sequences and unterminated strings

# Search for test cases involving escape sequences in strings
rg -n -A 5 -B 2 'NewLexer.*\\\\' pkg/dsl/lexer/lexer_test.go

# Search for test cases with unterminated strings
rg -n -A 5 -B 2 'unterminated|Unterminated' pkg/dsl/lexer/lexer_test.go

Repository: Permify/permify

Length of output: 680


🏁 Script executed:

# Get all test cases in the file to see the scope
rg -n 'It\(' pkg/dsl/lexer/lexer_test.go | head -20

Repository: Permify/permify

Length of output: 615


🏁 Script executed:

# Search for tests with actual escape sequences in test inputs
rg -n '\\\\n|\\\\t|\\\\x|\\\\z|\\\\\\\\' pkg/dsl/lexer/lexer_test.go | head -20

Repository: Permify/permify

Length of output: 41


🏁 Script executed:

# Get context around Case 11 and nearby test cases to see what else is tested
sed -n '1150,1250p' pkg/dsl/lexer/lexer_test.go

Repository: Permify/permify

Length of output: 1448


🏁 Script executed:

# Find and examine the lexer implementation to understand escape sequence handling
fd lexer.go pkg/dsl/lexer/ --type f

Repository: Permify/permify

Length of output: 82


🏁 Script executed:

# Search for escape sequence handling in the lexer
rg -n -B 3 -A 10 'escape|backslash' pkg/dsl/lexer/lexer.go | head -40

Repository: Permify/permify

Length of output: 823


🏁 Script executed:

# Get more of the lexString function to see complete escape handling
sed -n '205,280p' pkg/dsl/lexer/lexer.go

Repository: Permify/permify

Length of output: 1836


Add regression tests for other escape-sequence edge cases in unterminated strings.

The trailing backslash case (Case 11) is properly tested, but similar edge cases with escape sequences lack coverage. The lexer implementation has a safe if l.ch == 0 check that prevents panics, but the following scenarios should be explicitly tested:

  • Valid escape sequences at string end: "\n, "\t
  • Invalid escape sequences at string end: "\x, "\z
  • Multiple backslashes at string end: "\\, "\\\

Adding these tests ensures regression protection and documents the expected behavior for all unterminated string patterns.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/dsl/lexer/lexer_test.go` around lines 1193 - 1207, Add additional
regression test cases to the test file following the same pattern as the
existing "Case 11" test. Create separate test cases for valid escape sequences
at string end (such as "\n and "\t), invalid escape sequences at string end
(such as "\x and "\z), and multiple backslashes at string end (such as "\\ and
"\\\). Each new test case should follow the same structure as Case 11: create a
NewLexer with the unterminated string input, iterate through tokens with
NextToken() until reaching token.EOF, and verify that the operation does not
panic. This ensures comprehensive regression coverage for all unterminated
string patterns with escape sequences.

})
Loading