fix(dsl): prevent lexer panic on string ending in trailing backslash
Bug
The schema DSL lexer panics with a slice-out-of-range error when lexing an unterminated string that ends in a trailing backslash. Minimal repro:
NewParser(""\").Parse() panic: runtime error: slice bounds out of range [:3] with length 2 pkg/dsl/lexer/lexer.go:231
Reached from the SchemaWrite API via Parse(); the gRPC recovery interceptor catches it, so it errors the request instead of crashing the server, but it's still an uncontrolled panic on untrusted input.
Root cause
In lexString, when a \ is the last character of the input, l.readChar() (skipping the backslash) sets l.ch = 0 but the code still executes position = l.position + 1, advancing position one past len(input). The loop then exits on l.ch == 0, and the final str += l.input[position:l.position] slices with a start index greater than the string length, panicking.
Fix
Detect l.ch == 0 immediately after skipping the backslash and break early with position = l.position, treating it as an unterminated string instead of continuing to advance past the input bounds.
Testing
Added a regression test in lexer_test.go asserting the lexer doesn't panic on ""\", and verified the exact reporter NewParser(""\").Parse()) now returns cleanly. Full pkg/dsl/... suite still passes.
Closes #3004
Summary by CodeRabbit
-
Bug Fixes
- Fixed a crash in the DSL parser when processing string literals with unterminated escape sequences.
-
Tests
- Added test coverage to verify the parser handles malformed strings gracefully without panicking.
π Walkthrough
Walkthrough
The fix adds an end-of-input guard inside lexString in the DSL lexer: when a backslash is encountered and l.ch is 0 (EOF), the lexer updates its position and exits the escape loop instead of attempting to parse a non-existent escape character. A Ginkgo regression test confirms no panic occurs on the two-byte input "\.
Changes
Lexer panic fix for trailing backslash
| Layer / File(s) | Summary |
|---|---|
lexString EOF guard and regression test pkg/dsl/lexer/lexer.go, pkg/dsl/lexer/lexer_test.go |
lexString checks l.ch == 0 after a \ and breaks out of the escape loop instead of slicing past end-of-input; a Ginkgo test verifies the fix by iterating tokens on "\ until EOF and asserting no panic. |
Estimated code review effort
π― 2 (Simple) | β±οΈ ~5 minutes
Poem
A backslash sat at the end of the line, No char came after β a panic by design! But the rabbit hopped in with a nil-check so neat, Now the lexer just shrugs and accepts the defeat. No more slice-out-of-range, no more fright β π Unterminated strings handled just right!
π₯ Pre-merge checks | β 5
β Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title 'fix(dsl): prevent lexer panic on string ending in trailing backslash' clearly and specifically describes the main change: preventing a panic in the DSL lexer for strings with trailing backslashes. |
| Linked Issues check | β Passed | The changes fully address issue #3004: the fix detects unterminated strings ending with a backslash and breaks early to prevent out-of-bounds slicing, while the regression test verifies the lexer no longer panics on the problematic input. |
| Out of Scope Changes check | β Passed | All changes are directly scoped to fixing the reported bug: modifications to lexString function logic and addition of a regression test, with no extraneous changes detected. |
| Docstring Coverage | β Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
βοΈ Tip: You can configure your own custom pre-merge checks in the settings.
β¨ Finishing Touches
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
@omer-topal @ucatbas please have a look and lmk for any required changes
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 74.69%. Comparing base (70b1110) to head (08a3dd0).
:warning: Report is 26 commits behind head on master.
:x: Your project check has failed because the head coverage (74.69%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage.
:exclamation: There is a different number of reports uploaded between BASE (70b1110) and HEAD (08a3dd0). Click for more details.
HEAD has 2 uploads less than BASE
Flag BASE (70b1110) HEAD (08a3dd0) 3 1
Additional details and impacted files
@@ Coverage Diff @@
## master #3007 +/- ##
==========================================
- Coverage 82.58% 74.69% -7.89%
==========================================
Files 74 83 +9
Lines 8300 9215 +915
==========================================
+ Hits 6854 6882 +28
- Misses 910 1798 +888
+ Partials 536 535 -1
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.