Skip to content

Commit

Permalink
Add guard when converting from i64 to usize
Browse files Browse the repository at this point in the history
In some cases, when bad data is passed in as a set of change to
parinfer-rust it can cause parinfer-rust to crash. This crash is
caused by trying to add indents to a line. When the delta calculated
turns out to be negative and when that is added to orig_indent that
can cause issues when type casting to usize (Column). This is because
the combination of these two variables can be negative and when type
cast to a usize it causes the number to be very large, in my case
something like 1.8 trillion. So when this number is then used to
determine how many times to repeat a string, it causes the library to
run out of memory and crash.
  • Loading branch information
justinbarclay committed Jun 11, 2020
1 parent bcfdcd1 commit 67924ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parinfer_rust"
version = "0.4.3"
version = "0.4.4-beta"
authors = ["Jason Felice <[email protected]>"]

[lib]
Expand Down
17 changes: 16 additions & 1 deletion src/parinfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,21 @@ fn peek_works() {
assert_eq!(peek(&empty, 1), None);
}

fn delta_to_column(delta: Delta) -> Column {
if delta >= 0 {
delta as Column
} else {
0 as Column
}
}

#[cfg(test)]
#[test]
fn delta_to_column_works(){
assert_eq!(delta_to_column(1), 1);
assert_eq!(delta_to_column(0), 0);
assert_eq!(delta_to_column(-1), 0);
}
// {{{1 Questions about characters

fn is_open_paren(paren: &str) -> bool {
Expand Down Expand Up @@ -1429,7 +1444,7 @@ fn finish_new_paren_trail<'a>(result: &mut State<'a>) {

fn add_indent<'a>(result: &mut State<'a>, delta: Delta) {
let orig_indent = result.x;
let new_indent = (orig_indent as Delta + delta) as Column;
let new_indent = delta_to_column(orig_indent as Delta + delta);
let indent_str = repeat_string(BLANK_SPACE, new_indent);
let line_no = result.line_no;
replace_within_line(result, line_no, 0, orig_indent, &indent_str);
Expand Down

0 comments on commit 67924ee

Please sign in to comment.