* src/sort.c: Ensure each child is the sole writer of its
parent’s end_lo or end_hi.
* tests/sort/sort-merge-race.sh: Add a new hellgrind test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-UQ0LB5BK.
---
 NEWS                          |  6 ++++++
 src/sort.c                    | 28 ++++++++++++++++--------
 tests/local.mk                |  1 +
 tests/sort/sort-merge-race.sh | 40 +++++++++++++++++++++++++++++++++++
 4 files changed, 66 insertions(+), 9 deletions(-)
 create mode 100755 tests/sort/sort-merge-race.sh

diff --git a/NEWS b/NEWS
index f29045f68..779436b5f 100644
--- a/NEWS
+++ b/NEWS
@@ -327,6 +327,12 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   the same input file more than once.
   [bug introduced in coreutils-8.8]
 
+  'sort' with multiple threads no longer has a data race, where a thread
+  could use lines that another thread had not yet completely stored.
+  This could theoretically result in invalid memory access or incorrect
+  output on systems with weakly ordered memory, like aarch64.
+  [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 d687e27aa..3917b43e3 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -3857,10 +3857,15 @@ write_unique (struct line const *line, FILE *tfp, char 
const *temp_output)
    merge tree.  Merge a number of lines appropriate for this merge
    level, assuming TOTAL_LINES is the total number of lines.
 
-   If merging at the top level, send output to TFP.  TEMP_OUTPUT is
-   the name of TFP, or is null if TFP is standard output.  */
+   If merging at the top level, send output to TFP, and return NULL.
+   TEMP_OUTPUT is the name of TFP, or is null if TFP is standard output.
 
-static void
+   Otherwise, merge to the parent's buffer and return the new end of
+   the lines available there.  Do not store that into *NODE->dest,
+   as another thread may be merging the parent; the caller must
+   do so while holding the parent's lock.  */
+
+static struct line *
 mergelines_node (struct merge_node *restrict node, size_t total_lines,
                  FILE *tfp, char const *temp_output)
 {
@@ -3869,11 +3874,12 @@ mergelines_node (struct merge_node *restrict node, 
size_t total_lines,
   size_t to_merge = MAX_MERGE (total_lines, node->level);
   size_t merged_lo;
   size_t merged_hi;
+  struct line *dest = NULL;
 
   if (node->level > MERGE_ROOT)
     {
       /* Merge to destination buffer. */
-      struct line *dest = *node->dest;
+      dest = *node->dest;
       while (node->lo != node->end_lo && node->hi != node->end_hi && 
to_merge--)
         if (compare (node->lo - 1, node->hi - 1) <= 0)
           *--dest = *--node->lo;
@@ -3889,7 +3895,6 @@ mergelines_node (struct merge_node *restrict node, size_t 
total_lines,
       else if (node->nlo == merged_lo)
         while (node->hi != node->end_hi && to_merge--)
           *--dest = *--node->hi;
-      *node->dest = dest;
     }
   else
     {
@@ -3922,6 +3927,7 @@ mergelines_node (struct merge_node *restrict node, size_t 
total_lines,
   merged_hi = hi_orig - node->hi;
   node->nlo -= merged_lo;
   node->nhi -= merged_hi;
+  return dest;
 }
 
 /* Into QUEUE, insert NODE if it is not already queued, and if one of
@@ -3940,15 +3946,18 @@ queue_check_insert (struct merge_node_queue *queue, 
struct merge_node *node)
     }
 }
 
-/* Into QUEUE, insert NODE's parent if the parent can now be worked on.  */
+/* Make the lines that NODE has merged, which now end at DEST, available
+   to NODE's parent, and into QUEUE, insert the parent if it can now be
+   worked on.  DEST is ignored if NODE is the MERGE_ROOT node.  */
 
 static void
 queue_check_insert_parent (struct merge_node_queue *queue,
-                           struct merge_node *node)
+                           struct merge_node *node, struct line *dest)
 {
   if (node->level > MERGE_ROOT)
     {
       lock_node (node->parent);
+      *node->dest = dest;
       queue_check_insert (queue, node->parent);
       unlock_node (node->parent);
     }
@@ -3981,9 +3990,10 @@ merge_loop (struct merge_node_queue *queue,
           queue_insert (queue, node);
           break;
         }
-      mergelines_node (node, total_lines, tfp, temp_output);
+      struct line *dest = mergelines_node (node, total_lines, tfp,
+                                           temp_output);
       queue_check_insert (queue, node);
-      queue_check_insert_parent (queue, node);
+      queue_check_insert_parent (queue, node, dest);
 
       unlock_node (node);
     }
diff --git a/tests/local.mk b/tests/local.mk
index e0d0c949a..0c0aa0ee5 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -489,6 +489,7 @@ all_tests =                                 \
   tests/sort/sort-locale.sh                    \
   tests/sort/sort-merge.pl                     \
   tests/sort/sort-merge-fdlimit.sh             \
+  tests/sort/sort-merge-race.sh                        \
   tests/sort/sort-month.sh                     \
   tests/sort/sort-exit-early.sh                        \
   tests/sort/sort-rand.sh                      \
diff --git a/tests/sort/sort-merge-race.sh b/tests/sort/sort-merge-race.sh
new file mode 100755
index 000000000..b7b76e950
--- /dev/null
+++ b/tests/sort/sort-merge-race.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+# Ensure the threads of 'sort' synchronize when passing on merged lines.
+
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ sort
+require_valgrind_
+
+grep '^#define HAVE_PTHREAD_T 1' "$CONFIG_HEADER" > /dev/null ||
+  skip_ 'requires pthreads'
+
+# sort uses more than one thread only from 128Ki lines on.
+seq 200000 > in || framework_failure_
+
+# Alternate between the threads where supported,
+# as otherwise one of them often does all of the merging.
+helgrind='valgrind --tool=helgrind --fair-sched=try --error-exitcode=3'
+
+$helgrind sort --parallel=1 /dev/null > /dev/null 2>&1 ||
+  skip_ 'requires a working helgrind'
+
+# From 8.8 to 9.12 a thread stored merged lines for the parent node,
+# and their new end, without holding the lock of the parent node.
+$helgrind sort --parallel=2 in > /dev/null || fail=1
+
+Exit $fail
-- 
2.56.0


Reply via email to