Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ fn make_diff(
) -> Vec<Mismatch> {
let mut line_number_expected = 1;
let mut line_number_actual = 1;
// `context_size` comes straight from the command line and may be enormous.
// Neither file can have more lines than it has bytes, so a larger request
// already means "the whole file"; clamping keeps the arithmetic below in
// range and the preallocation proportional to the input.
let context_size = context_size.min(expected.len() + actual.len() + 1);
let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size);
let mut lines_since_mismatch = context_size + 1;
let mut results = Vec::new();
Expand Down
28 changes: 26 additions & 2 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ fn match_context_diff_params(
.or(captures.name("num3"));
if let Some(numvalue) = num {
if !numvalue.as_str().is_empty() {
context_count = Some(numvalue.as_str().parse::<usize>().unwrap());
// GNU accepts a count larger than the machine can represent and
// clamps it, so saturate rather than panicking on overflow.
context_count = Some(numvalue.as_str().parse::<usize>().unwrap_or(usize::MAX));
}
}
if param == "-C" {
Expand Down Expand Up @@ -320,7 +322,9 @@ fn match_unified_diff_params(
.or(captures.name("num3"));
if let Some(numvalue) = num {
if !numvalue.as_str().is_empty() {
context_count = Some(numvalue.as_str().parse::<usize>().unwrap());
// GNU accepts a count larger than the machine can represent and
// clamps it, so saturate rather than panicking on overflow.
context_count = Some(numvalue.as_str().parse::<usize>().unwrap_or(usize::MAX));
}
}
if param == "-U" {
Expand Down Expand Up @@ -916,4 +920,24 @@ mod tests {
.is_err());
}
}

#[test]
fn oversized_context_count_saturates() {
// A digit run too large for usize must clamp rather than panic.
for arg in [
"-u99999999999999999999",
"-c99999999999999999999",
"--unified=99999999999999999999",
"--context=99999999999999999999",
] {
let params = parse_params(
[os("diff"), os(arg), os("foo"), os("bar")]
.iter()
.cloned()
.peekable(),
)
.unwrap();
assert_eq!(params.context_count, usize::MAX);
}
}
}
5 changes: 5 additions & 0 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ fn make_diff(
) -> Vec<Mismatch> {
let mut line_number_expected = 1;
let mut line_number_actual = 1;
// `context_size` comes straight from the command line and may be enormous.
// Neither file can have more lines than it has bytes, so a larger request
// already means "the whole file"; clamping keeps the arithmetic below in
// range and the preallocation proportional to the input.
let context_size = context_size.min(expected.len() + actual.len() + 1);
let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size);
let mut lines_since_mismatch = context_size + 1;
let mut results = Vec::new();
Expand Down
29 changes: 29 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,35 @@ mod diff {
Ok(())
}

#[test]
fn oversized_context_count() -> Result<(), Box<dyn std::error::Error>> {
let mut file1 = NamedTempFile::new()?;
file1.write_all("a\nb\nc\n".as_bytes())?;
let mut file2 = NamedTempFile::new()?;
file2.write_all("a\nX\nc\n".as_bytes())?;

// A context count past usize, and one that fits but would overflow the
// queue's capacity in bytes. Both used to abort.
for option in [
"-u99999999999999999999",
"-c99999999999999999999",
"--unified=99999999999999999999",
"--context=99999999999999999999",
"-U600000000000000000",
"-C600000000000000000",
] {
let mut cmd = cargo_bin_cmd!("diffutils");
cmd.arg("diff")
.arg(option)
.arg(file1.path())
.arg(file2.path());
cmd.assert()
.code(predicate::eq(1))
.stdout(predicate::str::contains("X"));
}
Ok(())
}

#[test]
fn read_from_stdin() -> Result<(), Box<dyn std::error::Error>> {
let mut file1 = NamedTempFile::new()?;
Expand Down
Loading