1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #7036 from jfinkels/head-too-many-negative-bytes

head: fix subtraction underflow with --bytes=-N
This commit is contained in:
Sylvestre Ledru 2024-12-31 08:58:48 +01:00 committed by GitHub
commit d81d893964
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View file

@ -256,6 +256,7 @@ fn catch_too_large_numbers_in_backwards_bytes_or_lines(n: u64) -> Option<usize>
}
}
/// Print to stdout all but the last `n` bytes from the given reader.
fn read_but_last_n_bytes(input: &mut impl std::io::BufRead, n: u64) -> std::io::Result<()> {
if n == 0 {
//prints everything
@ -285,7 +286,7 @@ fn read_but_last_n_bytes(input: &mut impl std::io::BufRead, n: u64) -> std::io::
if total_read <= n {
// Fill the ring buffer without exceeding n bytes
let overflow = total_read - n;
let overflow = n - total_read;
ring_buffer.extend_from_slice(&buffer[..read - overflow]);
} else {
// Write the ring buffer and the part of the buffer that exceeds n

View file

@ -157,6 +157,23 @@ fn test_negative_byte_syntax() {
.stdout_is("");
}
#[test]
fn test_negative_bytes_greater_than_input_size_stdin() {
new_ucmd!()
.args(&["-c", "-2"])
.pipe_in("a")
.succeeds()
.no_output();
}
#[test]
fn test_negative_bytes_greater_than_input_size_file() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
at.write_bytes("f", b"a");
ts.ucmd().args(&["-c", "-2", "f"]).succeeds().no_output();
}
#[test]
fn test_negative_zero_lines() {
new_ucmd!()