Skip to content

Commit fbb6fcf

Browse files
committed
fixup! diff: reject a --tabsize too large for GNU diff
1 parent bbce911 commit fbb6fcf

3 files changed

Lines changed: 21 additions & 35 deletions

File tree

‎src/params.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ use crate::bre::Bre;
88
use crate::color::ColorWhen;
99
use crate::utils::fold_file_name_case;
1010

11-
/// Largest value accepted for `--tabsize`, matching GNU diff.
12-
const MAX_TABSIZE: usize = isize::MAX as usize - 3;
11+
/// Largest value accepted for `--tabsize`.
12+
/// Side-by-side mode computes `tabsize + GUTTER_WIDTH_MIN` (3) as an
13+
/// `isize` in `side_diff::Config::new`, so anything larger would overflow.
14+
/// GNU diff accepts the same maximum.
15+
pub(crate) const MAX_TABSIZE: usize = isize::MAX as usize - 3;
1316

1417
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
1518
pub enum Format {

‎src/side_diff.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,14 @@ mod tests {
12741274
assert_eq!(config.separator_pos, 3);
12751275
}
12761276

1277+
#[test]
1278+
fn test_max_tab_size() {
1279+
// `tab_size + GUTTER_WIDTH_MIN` must not overflow `isize`.
1280+
let config = create_config(130, crate::params::MAX_TABSIZE, false);
1281+
assert_eq!(config.sdiff_half_width, 0);
1282+
assert_eq!(config.sdiff_column_two_offset, 130);
1283+
}
1284+
12771285
#[test]
12781286
fn test_tab_size_larger_than_width() {
12791287
let config = create_config(5, 10, false);

‎src/utils.rs‎

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -440,41 +440,16 @@ mod tests {
440440
);
441441
}
442442

443-
/// A writer that fails once it has been given `limit` bytes.
444-
struct LimitedWriter {
445-
written: Vec<u8>,
446-
limit: usize,
447-
}
448-
449-
impl Write for LimitedWriter {
450-
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
451-
let n = buf.len().min(self.limit - self.written.len());
452-
if n == 0 {
453-
return Err(std::io::ErrorKind::WriteZero.into());
454-
}
455-
self.written.extend_from_slice(&buf[..n]);
456-
Ok(n)
457-
}
458-
459-
fn flush(&mut self) -> std::io::Result<()> {
460-
Ok(())
461-
}
462-
}
463-
464443
#[test]
465444
fn huge_tabsize() {
466-
// The spaces are streamed to the writer, so a huge tab size
467-
// neither overflows nor needs a huge buffer up front.
468-
for tabsize in [usize::MAX / 2, usize::MAX] {
469-
let mut output = LimitedWriter {
470-
written: Vec::new(),
471-
limit: 1000,
472-
};
473-
assert!(do_expand_tabs(&mut output, b"a\tb\tc", tabsize).is_err());
474-
let mut expected = b"a".to_vec();
475-
expected.resize(1000, b' ');
476-
assert_eq!(output.written, expected);
477-
}
445+
// The spaces are streamed to the writer, so the largest accepted
446+
// tab size neither overflows nor needs a huge buffer up front.
447+
let mut buf = [0u8; 1000];
448+
let mut output = &mut buf[..];
449+
assert!(do_expand_tabs(&mut output, b"a\tb\tc", crate::params::MAX_TABSIZE).is_err());
450+
let mut expected = b"a".to_vec();
451+
expected.resize(1000, b' ');
452+
assert_eq!(buf[..], expected[..]);
478453
}
479454
}
480455

0 commit comments

Comments
 (0)