Skip to content

xargs - stopping at the -E end-of-file string leaves nothing on seekable stdin for the next reader #1017

Open

Description

@sylvestre

When xargs stops reading at the -E/--eof logical end-of-file string and its standard
input is a regular file, GNU xargs moves the file offset back to just after the input it
actually used. A later process that shares the same stdin, e.g. xargs -E X ...; cat in
one shell, sees the rest of the file. uutils reads stdin in 8 KiB chunks and never gives
back the unused part, so the shared offset is left at the end of the file (or 8 KiB in).
The next reader gets nothing.

With a pipe neither implementation can give the data back, and both behave the same. The
difference is only for seekable input.

This makes the GNU test sv-bug-20273 fail (sh -c 'xargs -E2; cat' < file).

Reproduction

Rust (incorrect)

$ printf 'h1\nSTOP\nh2\nh3\n' > eof.in
$ sh -c './target/release/xargs -E STOP echo; cat' < eof.in
h1
# Exit code: 0

GNU (correct)

$ printf 'h1\nSTOP\nh2\nh3\n' > eof.in
$ sh -c 'LC_ALL=C /usr/bin/xargs -E STOP echo; cat' < eof.in
h1
h2
h3
# Exit code: 0

Test

#[test]
#[cfg(unix)]
fn xargs_eof_string_leaves_rest_of_seekable_stdin() {
    use std::io::Write;
    use std::process::{Command, Stdio};

    let mut input = tempfile::NamedTempFile::new().unwrap();
    input.write_all(b"h1\nSTOP\nh2\nh3\n").unwrap();

    let script = format!("{} -E STOP echo; cat", env!("CARGO_BIN_EXE_xargs"));
    let output = Command::new("sh")
        .args(["-c", &script])
        .stdin(Stdio::from(std::fs::File::open(input.path()).unwrap()))
        .output()
        .unwrap();
    assert!(output.status.success());
    assert_eq!(String::from_utf8_lossy(&output.stdout), "h1\nh2\nh3\n");
}

Exposed by the GNU findutils test suite: sv-bug-20273.

Activity

  1. sharmis-tp commented on Nov 17, 2026

    @sharmis-tp

    I'd like to work on this. The fix should seek stdin back to just past the last consumed line when -E matches on a seekable fd.

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

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions