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

Fix out of bounds access in parse_snippet #165

Merged
merged 2 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let last_tail_index = last.highlight_end.min(last.text.len()) - 1;

should do the same thing.

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/out_of_bounds.recorded.json").unwrap();
let expected_suggestions =
rustfix::get_suggestions_from_json(&json, &HashSet::new(), rustfix::Filter::Everything)
.unwrap();
assert!(expected_suggestions.is_empty());
}