diff options
author | Wilfred Hughes <me@wilfred.me.uk> | 2023-01-15 20:04:12 -0800 |
---|---|---|
committer | Wilfred Hughes <me@wilfred.me.uk> | 2023-01-15 20:04:12 -0800 |
commit | daa7156a2ca972638a61b479dc6fbe433e1ca98b (patch) | |
tree | ed027eba4f9cdd4af981d7de5da855e87d95e5ad | |
parent | e17b6e61092a60acde7e4071e4c44b80af073da5 (diff) |
Fix crash with --display=inline and trailing whitespace0.42.0
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
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/display/context.rs | 9 |
2 files changed, 10 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e96ac4e2f..88e8ceb36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/display/context.rs b/src/display/context.rs index 48cf21373..077a048c5 100644 --- a/src/display/context.rs +++ b/src/display/context.rs @@ -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![]); + } + } |