This repository was archived by the owner on Nov 24, 2023. It is now read-only.
File tree 3 files changed +63
-2
lines changed
3 files changed +63
-2
lines changed Original file line number Diff line number Diff line change @@ -114,11 +114,20 @@ fn parse_snippet(span: &DiagnosticSpan) -> Option<Snippet> {
114
114
}
115
115
let mut tail = String :: new ( ) ;
116
116
let last = & span. text [ span. text . len ( ) - 1 ] ;
117
+
118
+ // If we get a DiagnosticSpanLine where highlight_end > text.len(), we prevent an 'out of
119
+ // bounds' access by using text.len() - 1 instead.
120
+ let last_tail_index = if ( last. highlight_end - 1 ) > last. text . len ( ) {
121
+ last. text . len ( ) - 1
122
+ } else {
123
+ last. highlight_end - 1
124
+ } ;
125
+
117
126
if span. text . len ( ) > 1 {
118
127
body. push ( '\n' ) ;
119
- body. push_str ( & last. text [ indent..last . highlight_end - 1 ] ) ;
128
+ body. push_str ( & last. text [ indent..last_tail_index ] ) ;
120
129
}
121
- tail. push_str ( & last. text [ last . highlight_end - 1 ..] ) ;
130
+ tail. push_str ( & last. text [ last_tail_index ..] ) ;
122
131
Some ( Snippet {
123
132
file_name : span. file_name . clone ( ) ,
124
133
line_range : LineRange {
Original file line number Diff line number Diff line change
1
+ {
2
+ "message" : " unterminated double quote string" ,
3
+ "code" : null ,
4
+ "level" : " error" ,
5
+ "spans" : [
6
+ {
7
+ "file_name" : " ./tests/everything/tab_2.rs" ,
8
+ "byte_start" : 485 ,
9
+ "byte_end" : 526 ,
10
+ "line_start" : 12 ,
11
+ "line_end" : 13 ,
12
+ "column_start" : 7 ,
13
+ "column_end" : 3 ,
14
+ "is_primary" : true ,
15
+ "text" : [
16
+ {
17
+ "text" : " \"\"\" ; //~ ERROR unterminated double quote" ,
18
+ "highlight_start" : 7 ,
19
+ "highlight_end" : 45
20
+ },
21
+ {
22
+ "text" : " }" ,
23
+ "highlight_start" : 1 ,
24
+ "highlight_end" : 3
25
+ }
26
+ ],
27
+ "label" : null ,
28
+ "suggested_replacement" : null ,
29
+ "suggestion_applicability" : null ,
30
+ "expansion" : null
31
+ }
32
+ ],
33
+ "children" : [],
34
+ "rendered" : " error: unterminated double quote string\n --> ./tests/everything/tab_2.rs:12:7\n |\n 12 | \"\"\" ; //~ ERROR unterminated double quote\n | _______^\n 13 | | }\n | |__^\n\n "
35
+ }
36
+ {
37
+ "message" : " aborting due to previous error" ,
38
+ "code" : null ,
39
+ "level" : " error" ,
40
+ "spans" : [],
41
+ "children" : [],
42
+ "rendered" : " error: aborting due to previous error\n\n "
43
+ }
Original file line number Diff line number Diff line change @@ -10,3 +10,12 @@ fn multiple_fix_options_yield_no_suggestions() {
10
10
. unwrap ( ) ;
11
11
assert ! ( expected_suggestions. is_empty( ) ) ;
12
12
}
13
+
14
+ #[ test]
15
+ fn out_of_bounds_test ( ) {
16
+ let json = fs:: read_to_string ( "./tests/edge-cases/out_of_bounds.recorded.json" ) . unwrap ( ) ;
17
+ let expected_suggestions =
18
+ rustfix:: get_suggestions_from_json ( & json, & HashSet :: new ( ) , rustfix:: Filter :: Everything )
19
+ . unwrap ( ) ;
20
+ assert ! ( expected_suggestions. is_empty( ) ) ;
21
+ }
You can’t perform that action at this time.
0 commit comments