From 79063df07897dd24d9265b654add0105bafd4f54 Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Sat, 7 Nov 2026 17:33:23 -0800 Subject: [PATCH] sort: don't share compressed temp files for the same input when merging * src/sort.c (sortlines): Don't share compressed temporary files when the same input is given multiple times. * tests/sort/sort-compress-hang.sh: Add a test case that would previously hang. * NEWS: Mention the bug fix. Discovered by Anthropic's OSS Scanner, as bug ANT-2026-MYSN7JAW. --- NEWS | 4 ++++ src/sort.c | 21 ++++++++++++++++----- tests/sort/sort-compress-hang.sh | 21 ++++++++++++++++++++- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index d932371d7..f6e538bb8 100644 --- a/NEWS +++ b/NEWS @@ -190,6 +190,10 @@ GNU coreutils NEWS -*- outline -*- environment. [bug introduced in coreutils-9.9] + 'sort -m --compress-program=PROG -o FILE' no longer hangs when given + the same input file more than once. + [bug introduced in coreutils-8.8] + 'split --hex-suffixes=FROM' now generates consecutive suffixes when FROM contains any of the digits a to f. Previously it read beyond the suffix alphabet, giving arbitrary and repeated file names, thus losing output. diff --git a/src/sort.c b/src/sort.c index 962588c34..74f712b94 100644 --- a/src/sort.c +++ b/src/sort.c @@ -4103,9 +4103,10 @@ sortlines (struct line *restrict lines, size_t nthreads, /* Scan through FILES[NTEMPS .. NFILES-1] looking for files that are the same as OUTFILE. If found, replace each with the same temporary copy that can be merged into OUTFILE without destroying - OUTFILE before it is completely read. This temporary copy does not - count as a merge temp, so don't worry about incrementing NTEMPS in - the caller; final cleanup will remove it, not zaptemp. + OUTFILE before it is completely read, or with its own file if + temporary files are compressed. This temporary copy does not count + as a merge temp, so don't worry about incrementing NTEMPS in the + caller; final cleanup will remove it, not zaptemp. This test ensures that an otherwise-erroneous use like "sort -m -o FILE ... FILE ..." copies FILE before writing to it. @@ -4146,11 +4147,21 @@ avoid_trashing_input (struct sortfile *files, size_t ntemps, if (same) { - if (! tempcopy) + if (! tempcopy || tempcopy->state != UNCOMPRESSED) { FILE *tftp; + struct sortfile src = files[i]; + + /* Don't share compressed temporary files when the same input + file is given multiple times since each tempnode only + tracks a single PID. */ + if (tempcopy) + { + src.name = tempcopy->name; + src.temp = tempcopy; + } tempcopy = create_temp (&tftp); - mergefiles (&files[i], 0, 1, tftp, tempcopy->name); + mergefiles (&src, 0, 1, tftp, tempcopy->name); } files[i].name = tempcopy->name; diff --git a/tests/sort/sort-compress-hang.sh b/tests/sort/sort-compress-hang.sh index 537e94c06..971a5d88e 100755 --- a/tests/sort/sort-compress-hang.sh +++ b/tests/sort/sort-compress-hang.sh @@ -43,7 +43,7 @@ timeout --foreground 1700 sort --compress-program=./compress -S 1k in > out \ compare exp out || fail=1 test -f ok || fail=1 -rm -f compress ok +rm -f ok || framework_failure_ # If $TMPDIR is relative, give subprocesses time to react when 'sort' exits. # Otherwise, under NFS, when 'sort' unlinks the temp files and they @@ -54,4 +54,23 @@ case $TMPDIR in *) sleep 1;; esac +# Make sure that 'sort' doesn't hang when the output file is given as +# the input multiple times. +# FIXME: This assumes the input file is larger than the default pipe size. +seq -w 100000 >exp1 && +sed p exp1 >exp2 && +cp exp1 inp || framework_failure_ +timeout 60 sort -m --compress-program=./compress -o inp inp inp >out 2>err \ + || fail=1 +compare exp2 inp || fail=1 +compare /dev/null out || fail=1 +compare /dev/null errr || fail=1 +cp exp1 inp || framework_failure_ +timeout 60 sort -m --compress-program=./compress -o inp - - out 2>err \ + || fail=1 +compare exp2 inp || fail=1 +compare /dev/null out || fail=1 +compare /dev/null err || fail=1 +rm -f compress ok || framework_failure_ + Exit $fail