* src/fold.c (adjust_column): Clamp the column decrement to 0.
* tests/fold/fold-characters.sh: Add a test case.
* NEWS: Mention the bug fix.
---
 NEWS                          | 4 ++++
 src/fold.c                    | 5 +----
 tests/fold/fold-characters.sh | 5 +++++
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index 7af271ceb..a5e6ba57a 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   the \u0085 (next line) character as a separator on non-GLIBC platforms.
   [bug introduced in coreutils-9.11]
 
+  'fold' no longer prematurely wraps lines with a wide character towards
+  the start of a line, followed by multiple backspaces.
+  [bug introduced in coreutils-9.8]
+
   'realpath' without -s, or 'readlink' with -e,-f,-m no longer
   loop infinitely when resolving a symbolic link which
   resolves to a path beginning with the symbolic link itself.
diff --git a/src/fold.c b/src/fold.c
index f49078f01..2d725c3d5 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -117,10 +117,7 @@ adjust_column (size_t column, mcel_t g)
   if (counting_mode != COUNT_BYTES)
     {
       if (g.ch == '\b')
-        {
-          if (column > 0)
-            column -= last_character_width;
-        }
+        column -= MIN (last_character_width, column);
       else if (g.ch == '\r')
         column = 0;
       else if (g.ch == '\t')
diff --git a/tests/fold/fold-characters.sh b/tests/fold/fold-characters.sh
index 42fb8f737..cfdee2b3e 100755
--- a/tests/fold/fold-characters.sh
+++ b/tests/fold/fold-characters.sh
@@ -92,4 +92,9 @@ compare exp4 out4 || fail=1
 # Check bad character at EOF
 test $(env printf '\xC3' | fold | wc -c) = 1 || fail=1
 
+# Ensure backspace clamps at position 0
+# From v9.8 to v9.12 inclusive, an internal unsigned could wrap,
+# causing premature line wrapping.
+test $(env printf 'A\uB250\b\bB\n' | fold -w80 | wc -l) = 1 || fail=1
+
 Exit $fail
-- 
2.55.0


Reply via email to