Skip to content

Commit b123655

Browse files
authored
Fix fatal compiler error that occurred when an %ffi extension point contained invalid JavaScript (#7998)
* Check pos >= 0 and len >= 0 before doing String.sub in Code_frame.print * Add CHANGELOG
1 parent 5d8bdc1 commit b123655

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#### :bug: Bug fix
2424

25+
- Fix fatal compiler error that occurred when an `%ffi` extension point contained invalid JavaScript https://github.com/rescript-lang/rescript/pull/7998
26+
2527
#### :memo: Documentation
2628

2729
#### :nail_care: Polish

compiler/ml/code_frame.ml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,25 @@ let print ~is_warning ~src ~(start_pos : Lexing.position)
136136
(* 3 for separator + the 2 spaces around it *)
137137
let line_width = 78 - max_line_digits_count - indent - 3 in
138138
let lines =
139-
String.sub src start_line_line_offset
140-
(end_line_line_end_offset - start_line_line_offset)
141-
|> String.split_on_char '\n'
142-
|> filter_mapi (fun i line ->
143-
let line_number = i + first_shown_line in
144-
if more_than_5_highlighted_lines then
145-
if line_number = highlight_line_start_line + 2 then
146-
Some (Elided, line)
147-
else if
148-
line_number > highlight_line_start_line + 2
149-
&& line_number < highlight_line_end_line - 1
150-
then None
151-
else Some (Number line_number, line)
152-
else Some (Number line_number, line))
139+
if
140+
start_line_line_offset >= 0
141+
&& end_line_line_end_offset >= start_line_line_offset
142+
then
143+
String.sub src start_line_line_offset
144+
(end_line_line_end_offset - start_line_line_offset)
145+
|> String.split_on_char '\n'
146+
|> filter_mapi (fun i line ->
147+
let line_number = i + first_shown_line in
148+
if more_than_5_highlighted_lines then
149+
if line_number = highlight_line_start_line + 2 then
150+
Some (Elided, line)
151+
else if
152+
line_number > highlight_line_start_line + 2
153+
&& line_number < highlight_line_end_line - 1
154+
then None
155+
else Some (Number line_number, line)
156+
else Some (Number line_number, line))
157+
else []
153158
in
154159
let leading_space_to_cut =
155160
lines

0 commit comments

Comments
 (0)