Skip to content

Commit 97a81e5

Browse files
authored
Fix index out of bounds exception in rescript-editor-analysis codeAction (#7523)
* Check offset >= 0 before doing text.[offset] * Check offsetEnd >= offsetStart >=0 before doing `String.sub text offsetStart (offsetEnd - offsetStart)` * Ensure Pos.positionToOffset does not return negative offsets * Add CHANGELOG
1 parent 6f4275c commit 97a81e5

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
- `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497
2525
- Treat `throw` like `raise` in analysis. https://github.com/rescript-lang/rescript/pull/7521
26+
- Fix `index out of bounds` exception thrown in rare cases by `rescript-editor-analysis.exe codeAction` command. https://github.com/rescript-lang/rescript/pull/7523
2627

2728
#### :nail_care: Polish
2829

analysis/src/CompletionFrontEnd.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
373373
in
374374
let posOfDot = Pos.posOfDot text ~pos:posCursor ~offset in
375375
let charAtCursor =
376-
if offset < String.length text then text.[offset] else '\n'
376+
if offset >= 0 && offset < String.length text then text.[offset] else '\n'
377377
in
378378
let posBeforeCursor = Pos.posBeforeCursor posCursor in
379379
let charBeforeCursor, blankAfterCursor =
@@ -869,7 +869,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
869869
match
870870
(Pos.positionToOffset text posStart, Pos.positionToOffset text posEnd)
871871
with
872-
| Some offsetStart, Some offsetEnd ->
872+
| Some offsetStart, Some offsetEnd
873+
when offsetStart >= 0 && offsetEnd >= offsetStart ->
873874
(* Can't trust the parser's location
874875
E.g. @foo. let x... gives as label @foo.let *)
875876
let label =

analysis/src/Pos.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ let positionToOffset text (line, character) =
2222
match offsetOfLine text line with
2323
| None -> None
2424
| Some bol ->
25-
if bol + character <= String.length text then Some (bol + character)
26-
else None
25+
let offset = bol + character in
26+
if offset >= 0 && offset <= String.length text then Some offset else None
2727

2828
let posBeforeCursor pos = (fst pos, max 0 (snd pos - 1))
2929

0 commit comments

Comments
 (0)