This avoids the following behavior:
$ strace -ff -e trace=none sh -c 'echo a > fifo'
--- SIGPIPE {si_signo=SIGPIPE, ...} ---
+++ killed by SIGPIPE +++
Where the SIGPIPE is caused by opening and closing the file descriptor
for --reference in the following command:
$ truncate --reference=fifo file
* src/truncate.c (main): Set errno to ESPIPE if the reference file is a
FIFO and skip opening a file descriptor.
* tests/truncate/truncate-fifo.sh: Add a test case.
* NEWS: Mention the bug fix.
Discovered by Anthropic's OSS Scanner, as bug ANT-2026-4YA4REOC.
---
NEWS | 5 +++++
src/truncate.c | 12 +++++++++++-
tests/truncate/truncate-fifo.sh | 20 ++++++++++++++++++++
3 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/NEWS b/NEWS
index c19438e5f..6f510a1ba 100644
--- a/NEWS
+++ b/NEWS
@@ -412,6 +412,11 @@ GNU coreutils NEWS -*-
outline -*-
generates a SIGPIPE or SIGXFSZ signal.
[bug introduced in coreutils-9.10]
+ 'truncate --reference=R' now avoids opening R when it is a FIFO. Previously,
+ it briefly opened it for reading, causing another process that opened it for
+ writing to receive a SIGPIPE signal.
+ [bug introduced in coreutils-8.17]
+
'unexpand' no longer overflows a heap buffer on non-GLIBC platforms,
given whitespace characters whose display width is negative. E.g.,
this edge case can occur with \u0085 (next line) characters on musl.
diff --git a/src/truncate.c b/src/truncate.c
index 7459a026a..4159109e4 100644
--- a/src/truncate.c
+++ b/src/truncate.c
@@ -325,9 +325,19 @@ main (int argc, char **argv)
error (EXIT_FAILURE, errno, _("cannot stat %s"), quoteaf (ref_file));
if (usable_st_size (&sb))
file_size = sb.st_size;
+ else if (S_ISFIFO (sb.st_mode))
+ {
+ /* We know that a subsequent lseek on REF_FILE, assuming it is not
+ replaced before calling open, would fail with ESPIPE. Set errno
+ here instead of opening REF_FILE to avoid disturbing any process
+ that has opened the REF_FILE FIFO for writing and is waiting for
+ a reader. */
+ errno = ESPIPE;
+ }
else
{
- /* O_NONBLOCK so we do not hang forever on a FIFO with no writer. */
+ /* Use O_NONBLOCK, since REF_FILE may have been replaced with a
+ FIFO following the previous stat call. */
int ref_fd = open (ref_file, O_RDONLY | O_NONBLOCK);
if (0 <= ref_fd)
{
diff --git a/tests/truncate/truncate-fifo.sh b/tests/truncate/truncate-fifo.sh
index 27a094f24..bdf7aa8ba 100755
--- a/tests/truncate/truncate-fifo.sh
+++ b/tests/truncate/truncate-fifo.sh
@@ -18,6 +18,7 @@
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ truncate
+getlimits_
mkfifo_or_skip_ fifo
@@ -30,4 +31,23 @@ touch file || framework_failure_
timeout 10 truncate --reference=fifo file 2>/dev/null
test "$?" = 124 && fail=1
+rm -f file &&
+echo "truncate: cannot get the size of 'fifo': $ESPIPE" >exp-err &&
+echo a >exp-fifo-out || framework_failure_
+# Terminate any background process.
+cleanup_() { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid=; }
+timeout 10 "$SHELL" -c 'cat exp-fifo-out >fifo' & pid=$!
+# Try to give the shell some time to open the fifo for writing.
+# Note that the test will still pass if it takes longer, but it will
+# not be very useful.
+sleep .1
+returns_ 1 truncate --reference=fifo file >out 2>err || fail=1
+timeout 10 cat fifo >fifo-out || fail=1
+wait $pid || fail=1
+pid=
+compare /dev/null out || fail=1
+compare exp-err err || fail=1
+compare exp-fifo-out fifo-out || fail=1
+returns_ 1 test -f fifo || fail=1
+
Exit $fail
--
2.56.1