Fix crash with --display=inline and trailing whitespace

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
pull/464/head 0.42.0
Wilfred Hughes 2023-01-15 20:04:12 +07:00
parent e17b6e6109
commit daa7156a2c
2 changed files with 10 additions and 1 deletions

@ -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

@ -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;
}
@ -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![]);
}
}