truncate: don't hang with --reference=FIFO

* NEWS: Mention the fix.
* src/truncate.c (main): Open the reference with O_NONBLOCK.
* tests/truncate/truncate-fifo.sh: Also cover --reference=FIFO.
This commit is contained in:
Iván Ezequiel Rodriguez authored and Collin Funk committed 2026-08-13 22:32:56 -07:00
1 parent 162ddbe972
commit 95de5f1372
3 files changed
+11 -1

No files matched your search

+3
View File
@@ -58,6 +58,9 @@ GNU coreutils NEWS -*- outline -*-
with an error.
[This bug was present in "the beginning".]
'truncate --reference=R' no longer hangs when R is a FIFO with no readers.
[bug introduced in coreutils-8.17]
'unexpand -t' no longer overflows a heap buffer, for tab values > SIZE_MAX/16,
or with multi-byte blank characters longer than the tab value.
[bugs introduced in coreutils-9.11]
+2 -1
View File
@@ -326,7 +326,8 @@ main (int argc, char **argv)
file_size = sb.st_size;
else
{
int ref_fd = open (ref_file, O_RDONLY);
/* O_NONBLOCK so we do not hang forever on a FIFO with no writer. */
int ref_fd = open (ref_file, O_RDONLY | O_NONBLOCK);
if (0 <= ref_fd)
{
off_t file_end = lseek (ref_fd, 0, SEEK_END);
+6
View File
@@ -21,7 +21,13 @@ print_ver_ truncate
mkfifo_or_skip_ fifo
# Output FIFO must not hang.
timeout 10 truncate -s0 fifo
test "$?" = 124 && fail=1
# Reference FIFO must not hang either.
touch file || framework_failure_
timeout 10 truncate --reference=fifo file 2>/dev/null
test "$?" = 124 && fail=1
Exit $fail