It would be good to add a test case for 'sync' and 'truncate', but it isn't as simple as 'csplit'. I guess maybe using 'strace' to inject a sync(2) or ftruncate(2) failure, respectively?
-- 8< -- * src/csplit.c: Include fcntl--.h. * src/sync.c: Likewise. * src/truncate.c: Likewise. * tests/csplit/csplit.sh: Add a test case that would fail after unrealeased commit 2d02d0821 (csplit: don't truncate the input when an output file is the input, 2026-10-08). * NEWS: Mention the 'sync' and 'truncate' bug fixes. Discovered by Anthropic's OSS Scanner, as bug ANT-2026-EM00DLR0. --- NEWS | 6 ++++++ src/csplit.c | 1 + src/sync.c | 1 + src/truncate.c | 1 + tests/csplit/csplit.sh | 23 ++++++++++++++++++++++- 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index dc24ae9b9..ec0b29111 100644 --- a/NEWS +++ b/NEWS @@ -342,6 +342,12 @@ GNU coreutils NEWS -*- outline -*- it would have failed if the control flag encoding was different. [This bug was present in "the beginning".] + 'sync' and 'truncate' now ensure that opened file descriptors do not + become standard input, output, or error. Previously, a system call + failure could be written to a file, for example, if a system call + failed. + [This bug was present in "the beginning".] + 'tac' no longer silently drops data from files that report a size smaller than their contents, or the edge case of seekable files that return short reads. [bug introduced in coreutils-8.24] diff --git a/src/csplit.c b/src/csplit.c index 353741905..22c534317 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -28,6 +28,7 @@ #include <regex.h> #include "c-ctype.h" +#include "fcntl--.h" #include "fd-reopen.h" #include "quote.h" #include "safe-read.h" diff --git a/src/sync.c b/src/sync.c index d03bb00a9..df4c4b887 100644 --- a/src/sync.c +++ b/src/sync.c @@ -23,6 +23,7 @@ #include "system.h" #include "assure.h" +#include "fcntl--.h" /* The official name of this program (e.g., no 'g' prefix). */ #define PROGRAM_NAME "sync" diff --git a/src/truncate.c b/src/truncate.c index e6c20b70a..7459a026a 100644 --- a/src/truncate.c +++ b/src/truncate.c @@ -26,6 +26,7 @@ #include <sys/types.h> #include "system.h" +#include "fcntl--.h" #include "quote.h" #include "stat-size.h" #include "xdectoint.h" diff --git a/tests/csplit/csplit.sh b/tests/csplit/csplit.sh index 2ec08a569..0e7394ebf 100755 --- a/tests/csplit/csplit.sh +++ b/tests/csplit/csplit.sh @@ -17,7 +17,7 @@ # along with this program. If not, see <https://www.gnu.org/licenses/>. . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src -print_ver_ csplit +print_ver_ csplit test # csplit could get a failed assertion to 2.0.17 @@ -197,4 +197,25 @@ vm=$(get_min_ulimit_v_ csplit -z f %b%1) && { fi } +# Ensure output files are not opened on a closed standard descriptor, +# otherwise diagnostics or byte counts would be written to the files. +printf '%s\n' '#a' 1 >exp-xx01 && +printf '%s\n' '#b' 2 >exp-xx02 && +cat exp-xx01 exp-xx02 >in || framework_failure_ + +# 2>&- and >&- are not effective on some systems. +if env test -w /dev/stderr 2>/dev/null && env test ! -w /dev/stderr 2>&-; then + rm -f xx?? || framework_failure_ + returns_ 1 csplit -k -s in '/^#/' '{9}' 2>&- || fail=1 + compare /dev/null xx00 || fail=1 + compare exp-xx01 xx01 || fail=1 + compare exp-xx02 xx02 || fail=1 +fi +if env test -w /dev/stdout >/dev/null && env test ! -w /dev/stdout >&-; then + rm -f xx?? || framework_failure_ + returns_ 1 csplit -k in '/#b/' '/2/-9' >&- 2>err || fail=1 + compare exp-xx01 xx00 || fail=1 + compare /dev/null xx01 || fail=1 +fi + Exit $fail -- 2.56.1
