Skip to content

Commit

Permalink
Fix crash with --display=inline and trailing whitespace
Browse files Browse the repository at this point in the history
Line numbers may be less than .max_line(), as .max_line() trims
whitespace. Ensure pad_after() is robust to this, and add a test.

I could only reproduce the crash in inline display mode, but in
principle this could be an issue in all modes.

Fixes #452
  • Loading branch information
Wilfred committed Jan 16, 2023
1 parent e17b6e6 commit daa7156
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ have few lines in common.
Fixed an issue with unwanted underlines with textual diffing when
DFT_BYTE_LIMIT is reached.

Fixed a crash in inline display when the file ends with whitespace.

### Build

Renamed the vendored parser directory to vendored_parsers/, as `cargo
Expand Down
9 changes: 8 additions & 1 deletion src/display/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ fn pad_after(ln: LineNumber, max_line: LineNumber, num_context_lines: usize) ->
// Use one more line than num_context_lines so we merge
// immediately adjacent hunks.
for _ in 0..num_context_lines + 1 {
if current == max_line {
if current >= max_line {
break;
}

Expand Down Expand Up @@ -1034,4 +1034,11 @@ mod tests {
]
);
}

#[test]
fn test_pad_after_when_line_exceeds_max() {
let res = pad_after(2.into(), 1.into(), 5);
assert_eq!(res, vec![]);
}

}

0 comments on commit daa7156

Please sign in to comment.