Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
Fix out of bounds access
Browse files Browse the repository at this point in the history
  • Loading branch information
phansch committed Mar 24, 2019
1 parent 2f5f030 commit 87949a5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,20 @@ fn parse_snippet(span: &DiagnosticSpan) -> Option<Snippet> {
}
let mut tail = String::new();
let last = &span.text[span.text.len() - 1];

// If we get a DiagnosticSpanLine where highlight_end > text.len(), we prevent an 'out of
// bounds' access by using text.len() - 1 instead.
let last_tail_index = if (last.highlight_end - 1) > last.text.len() {
last.text.len() - 1
} else {
last.highlight_end - 1
};

if span.text.len() > 1 {
body.push('\n');
body.push_str(&last.text[indent..last.highlight_end - 1]);
body.push_str(&last.text[indent..last_tail_index]);
}
tail.push_str(&last.text[last.highlight_end - 1..]);
tail.push_str(&last.text[last_tail_index..]);
Some(Snippet {
file_name: span.file_name.clone(),
line_range: LineRange {
Expand Down
43 changes: 43 additions & 0 deletions tests/edge-cases/out_of_bounds.recorded.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"message": "unterminated double quote string",
"code": null,
"level": "error",
"spans": [
{
"file_name": "./tests/everything/tab_2.rs",
"byte_start": 485,
"byte_end": 526,
"line_start": 12,
"line_end": 13,
"column_start": 7,
"column_end": 3,
"is_primary": true,
"text": [
{
"text": " \"\"\"; //~ ERROR unterminated double quote",
"highlight_start": 7,
"highlight_end": 45
},
{
"text": "}",
"highlight_start": 1,
"highlight_end": 3
}
],
"label": null,
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
}
],
"children": [],
"rendered": "error: unterminated double quote string\n --> ./tests/everything/tab_2.rs:12:7\n |\n12 | \"\"\"; //~ ERROR unterminated double quote\n | _______^\n13 | | }\n | |__^\n\n"
}
{
"message": "aborting due to previous error",
"code": null,
"level": "error",
"spans": [],
"children": [],
"rendered": "error: aborting due to previous error\n\n"
}
9 changes: 9 additions & 0 deletions tests/edge_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ fn multiple_fix_options_yield_no_suggestions() {
.unwrap();
assert!(expected_suggestions.is_empty());
}

#[test]
fn out_of_bounds_test() {
let json = fs::read_to_string("./tests/edge-cases/tab_2.recorded.json").unwrap();
let expected_suggestions =
rustfix::get_suggestions_from_json(&json, &HashSet::new(), rustfix::Filter::Everything)
.unwrap();
assert!(expected_suggestions.is_empty());
}

0 comments on commit 87949a5

Please sign in to comment.