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.
This commit is contained in:
Collin Funk committed 2026-11-08 12:01:22 -08:00
1 parent 5b86ff66eb
commit 79063df078
3 files changed
+40 -6

No files matched your search

+4
View File
@@ -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.
+16 -5
View File
@@ -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;
+20 -1
View File
@@ -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 - - <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