Skip to content

Commit 67924ee

Browse files
committed
Add guard when converting from i64 to usize
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.
1 parent bcfdcd1 commit 67924ee

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "parinfer_rust"
3-
version = "0.4.3"
3+
version = "0.4.4-beta"
44
authors = ["Jason Felice <jason.m.felice@gmail.com>"]
55

66
[lib]

src/parinfer.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,21 @@ fn peek_works() {
579579
assert_eq!(peek(&empty, 1), None);
580580
}
581581

582+
fn delta_to_column(delta: Delta) -> Column {
583+
if delta >= 0 {
584+
delta as Column
585+
} else {
586+
0 as Column
587+
}
588+
}
589+
590+
#[cfg(test)]
591+
#[test]
592+
fn delta_to_column_works(){
593+
assert_eq!(delta_to_column(1), 1);
594+
assert_eq!(delta_to_column(0), 0);
595+
assert_eq!(delta_to_column(-1), 0);
596+
}
582597
// {{{1 Questions about characters
583598

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

14301445
fn add_indent<'a>(result: &mut State<'a>, delta: Delta) {
14311446
let orig_indent = result.x;
1432-
let new_indent = (orig_indent as Delta + delta) as Column;
1447+
let new_indent = delta_to_column(orig_indent as Delta + delta);
14331448
let indent_str = repeat_string(BLANK_SPACE, new_indent);
14341449
let line_no = result.line_no;
14351450
replace_within_line(result, line_no, 0, orig_indent, &indent_str);

0 commit comments

Comments
 (0)