Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out of bounds access in parse_snippet #165

Merged
merged 2 commits into from Mar 26, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+59 −2
Diff settings

Always

Just for now

@@ -114,11 +114,16 @@ 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 making sure the index is within the array bounds.
let last_tail_index = last.highlight_end.min(last.text.len()) - 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 {
@@ -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"
}
@@ -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());
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.