* src/unexpand.c (unexpand): Keep track of the length of the first
pending blank, and compare the number of pending bytes to it instead of
one.
* tests/unexpand/mb.sh: Add a test case.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-UGL57PCQ.
---
 NEWS                 |  4 ++++
 src/unexpand.c       | 24 +++++++++++++++++++-----
 tests/unexpand/mb.sh | 19 +++++++++++++++++++
 3 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index 00cf3d830..286bc2683 100644
--- a/NEWS
+++ b/NEWS
@@ -382,6 +382,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   this edge case can occur with \u0085 (next line) characters on musl.
   [bug introduced in coreutils-9.11]
 
+  'unexpand -a' no longer corrupts a multi-byte blank character that ends on a
+  tab stop by replacing only its first byte with a tab.
+  [bug introduced in coreutils-9.11]
+
   'wc' no longer miscounts characters or words when a multi-byte character
   spans input buffers.  E.g. this could affect character counts
   in GB18030 locales, and word counts in UTF-8 locales.
diff --git a/src/unexpand.c b/src/unexpand.c
index 630be3613..0f73367a0 100644
--- a/src/unexpand.c
+++ b/src/unexpand.c
@@ -157,12 +157,15 @@ unexpand (void)
       /* If true, the first pending blank came just before a tab stop.  */
       bool one_blank_before_tab_stop = false;
 
+      /* Number of bytes in that first pending blank.  */
+      idx_t first_blank_len = 0;
+
       /* If true, the previous input character was a blank.  This is
          initially true, since initial strings of blanks are treated
          as if the line was preceded by a blank.  */
       bool prev_blank = true;
 
-      /* Number of pending columns of blanks.  */
+      /* Number of pending bytes of blanks.  */
       idx_t pending = 0;
 
 
@@ -207,7 +210,10 @@ unexpand (void)
                               /* It is not yet known whether the pending blanks
                                  will be replaced by tabs.  */
                               if (column == next_tab_column)
-                                one_blank_before_tab_stop = true;
+                                {
+                                  one_blank_before_tab_stop = true;
+                                  first_blank_len = g.len;
+                                }
                               memcpy (pending_blank + pending,
                                       mbbuf_char_offset (&mbbuf, g), g.len);
                               pending += g.len;
@@ -244,9 +250,17 @@ unexpand (void)
 
               if (pending)
                 {
-                  if (pending > 1 && one_blank_before_tab_stop)
-                    pending_blank[0] = '\t';
-                  if (fwrite (pending_blank, 1, pending, stdout) != pending)
+                  char const *p = pending_blank;
+                  if (first_blank_len < pending && one_blank_before_tab_stop)
+                    {
+                      /* More blanks followed the first one, so replace
+                         all of its bytes with a tab.  */
+                      if (putc ('\t', stdout) < 0)
+                        write_error ();
+                      p += first_blank_len;
+                      pending -= first_blank_len;
+                    }
+                  if (fwrite (p, 1, pending, stdout) != pending)
                     write_error ();
                   pending = 0;
                   one_blank_before_tab_stop = false;
diff --git a/tests/unexpand/mb.sh b/tests/unexpand/mb.sh
index b5e4a0b3a..33591ad32 100755
--- a/tests/unexpand/mb.sh
+++ b/tests/unexpand/mb.sh
@@ -93,6 +93,25 @@ exp='    ö        ü       ß'
 unexpand -a < in > out || fail=1
 compare exp out > /dev/null 2>&1 || fail=1
 
+# Test that a multi-byte blank ending on a tab is kept intact when it is
+# the only blank.  coreutils-9.11 and coreutils-9.12 would replace the
+# first byte of the blank with a tab and emit the rest of the bytes
+# unchanged.  Also, check that all bytes of a multi-byte blank are
+# replaced when more blanks follow it.
+env printf 'xxxxxx\u3000|
+xxxxxxx\u2003|
+xxxxxx\u3000 |
+xxxxxx\u3000\u3000|
+' > in &&
+env printf 'xxxxxx\u3000|
+xxxxxxx\u2003|
+xxxxxx\t |
+xxxxxx\t\u3000|
+' > exp || framework_failure_
+
+unexpand -a < in > out || fail=1
+compare exp out || fail=1
+
 #non-Unicode characters interspersed between Unicode ones
 env printf '12345678
         \xFF|
-- 
2.56.1


Reply via email to