From 62aa0f5fef2751d5d33e2a38d711a31e7b087bbb Mon Sep 17 00:00:00 2001 From: fixfon <12512757+fixfon@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:44:19 +0300 Subject: [PATCH] tee: handle closed stdin Refs #10154 --- src/uu/tee/src/tee.rs | 9 +++++++++ tests/by-util/test_tee.rs | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/uu/tee/src/tee.rs b/src/uu/tee/src/tee.rs index 3b87b2cb076..29409fff26f 100644 --- a/src/uu/tee/src/tee.rs +++ b/src/uu/tee/src/tee.rs @@ -147,6 +147,15 @@ impl MultiWriter { #[cfg(not(any(unix, target_os = "wasi")))] use io::Read as _; const BUF_SIZE: usize = 32 * 1024; + #[cfg(all(unix, not(target_os = "fuchsia")))] + if uucore::signals::stdin_was_closed() { + let e = Error::from(rustix::io::Errno::BADF); + show_error!( + "{}", + translate!("tee-error-stdin", "error" => strip_errno(&e)) + ); + return Err(()); + } #[cfg(any(unix, target_os = "wasi"))] let input = rustix::stdio::stdin(); #[cfg(not(any(unix, target_os = "wasi")))] diff --git a/tests/by-util/test_tee.rs b/tests/by-util/test_tee.rs index 3595e22c8ed..8f7b7e455e4 100644 --- a/tests/by-util/test_tee.rs +++ b/tests/by-util/test_tee.rs @@ -26,6 +26,21 @@ fn test_error_stdin_directory() { .stderr_is("tee: read error: Is a directory\n"); } +#[test] +#[cfg(all(unix, not(wasi_runner)))] +fn test_closed_stdin() { + // Reading a closed stdin fails with EBADF. Run through a shell so the + // process really starts with fd 0 closed. + let (at, ts) = ::uutests::at_and_ts!(); + ts.cmd("sh") + .args(&["-c", "exec <&-; \"$0\" tee file"]) + .arg(&ts.bin_path) + .fails_with_code(1) + .no_stdout() + .stderr_is("tee: read error: Bad file descriptor\n"); + assert_eq!(at.read("file"), ""); +} + #[test] fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails_with_code(1);