Skip to content

tail: arithmetic overflow (overflow-checks) on -c -N with N = 2^63 (i64::MIN negate) #14353

Description

@leeewee

tail -c -9223372036854775808 FILE panics with attempt to negate with overflow under overflow-checks.

$ yes | head -c 8192 > f      # a regular file LARGER than the block size (~4 KiB)
$ # debug build (overflow-checks on by default) — clean-rebuild to avoid a stale binary
$ cargo build -q -p uu_tail --bin tail
$ ./target/debug/tail -c -9223372036854775808 f >/dev/null
thread 'main' panicked at src/uu/tail/src/tail.rs:472:17:
attempt to negate with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ echo $?
101

Root cause

In bounded_tail, the Bytes(Negative(count)) arm computes the seek offset as -(*count as i64); for count = 9223372036854775808 (2^63), count as i64 is i64::MIN, and negating i64::MIN overflows i64. A debug (or release + -C overflow-checks) build aborts (exit 101); a normal release build wraps silently and exits 0.

FilterMode::Bytes(Signum::Negative(count)) => {
if file.seek(SeekFrom::End(-(*count as i64))).is_err() {
file.seek(SeekFrom::Start(0)).unwrap();
}
limit = Some(*count);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions