A tab list such as -t '/0,+5' was silently accepted, since
set_extend_size() and set_increment_size() detected a repeated specifier
by testing whether the stored size was nonzero. A first specifier with a
value of 0 left the size at zero, so the documented diagnostic never
fired. Likewise validate_tab_stops() missed combining '/0' with '+5', as
it used the same test to decide whether each specifier was present.

Track presence with separate extend_seen and increment_seen flags, which
also covers specifiers given in separate -t options, since
parse_tab_stops() is called once per option. A lone '/0' or '+0' is
still accepted and means "no extension", as before.

* src/expand-common.c (extend_seen, increment_seen): New globals.
(set_extend_size, set_increment_size): Use them to detect repeats.
(validate_tab_stops): Use them to find mutually exclusive specifiers.
* tests/expand/expand.pl: Add cases e8 to e14.
* tests/unexpand/unexpand.pl: Add cases spec-zero1 to spec-zero3.
* NEWS: Mention the bug fix.
---
 NEWS                       |  5 +++++
 src/expand-common.c        | 30 +++++++++++++++++++++---------
 tests/expand/expand.pl     | 18 ++++++++++++++++++
 tests/unexpand/unexpand.pl |  8 ++++++++
 4 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/NEWS b/NEWS
index d25d187e8..189f9df9c 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
 
 ** Bug fixes
 
+  'expand' and 'unexpand' now diagnose a tab list containing more than one
+  '/' or '+' specifier, even when the first such specifier has a value of 0.
+  Previously a tab list like -t '/0,+5' was silently accepted.
+  [bug introduced in coreutils-8.27]
+
   'basenc --base32hex' no longer decodes BASE32 only characters "WXYZ".
   [bug introduced with the basenc program in coreutils-8.31]
 
diff --git a/src/expand-common.c b/src/expand-common.c
index 0cf84678b..0e4e58ab5 100644
--- a/src/expand-common.c
+++ b/src/expand-common.c
@@ -38,6 +38,12 @@ static colno extend_size = 0;
 /* If nonzero, an increment for additional tab stops after the last 
specified.*/
 static colno increment_size = 0;
 
+/* Whether a '/' or '+' specifier was already seen.  These are tracked
+   separately from 'extend_size' and 'increment_size', as those may be
+   zero, and so can't distinguish "not specified" from "specified as 0".  */
+static bool extend_seen = false;
+static bool increment_seen = false;
+
 /* The maximum distance between tab stops.  */
 idx_t max_column_width;
 
@@ -98,16 +104,19 @@ set_extend_size (colno tabval)
 {
   bool ok = true;
 
-  if (extend_size)
+  if (extend_seen)
     {
       error (0, 0,
              _("'/' specifier only allowed"
                " with the last value"));
       ok = false;
     }
-  extend_size = tabval;
-
-  set_max_column_width (extend_size);
+  else
+    {
+      extend_seen = true;
+      extend_size = tabval;
+      set_max_column_width (extend_size);
+    }
 
   return ok;
 }
@@ -117,16 +126,19 @@ set_increment_size (colno tabval)
 {
   bool ok = true;
 
-  if (increment_size)
+  if (increment_seen)
     {
       error (0,0,
              _("'+' specifier only allowed"
                " with the last value"));
       ok = false;
     }
-  increment_size = tabval;
-
-  set_max_column_width (increment_size);
+  else
+    {
+      increment_seen = true;
+      increment_size = tabval;
+      set_max_column_width (increment_size);
+    }
 
   return ok;
 }
@@ -252,7 +264,7 @@ validate_tab_stops (colno const *tabs, idx_t entries)
       prev_tab = tabs[i];
     }
 
-  if (increment_size && extend_size)
+  if (increment_seen && extend_seen)
     error (EXIT_FAILURE, 0, _("'/' specifier is mutually exclusive with '+'"));
 }
 
diff --git a/tests/expand/expand.pl b/tests/expand/expand.pl
index 7ce3bc409..488f771e1 100755
--- a/tests/expand/expand.pl
+++ b/tests/expand/expand.pl
@@ -199,6 +199,24 @@ my @Tests =
    ['e7', '--tabs=3/', {IN=>''}, {OUT=>''}, {EXIT=>1},
     {ERR => "$prog: '/' specifier not at start of number: '/'\n"}],
 
+   # A specifier with a zero value must still be counted, both within
+   # one tab list and across separate -t options.
+   ['e8', '--tabs=/0,5', {IN=>''}, {OUT=>''}, {EXIT=>1},
+    {ERR => "$prog: '/' specifier only allowed with the last value\n"}],
+   ['e9', '--tabs=+0,+5', {IN=>''}, {OUT=>''}, {EXIT=>1},
+    {ERR => "$prog: '+' specifier only allowed with the last value\n"}],
+   ['e10', '--tabs=/0,+5', {IN=>''}, {OUT=>''}, {EXIT=>1},
+    {ERR => "$prog: '/' specifier is mutually exclusive with '+'\n"}],
+   ['e11', '-t/0 -t/5', {IN=>''}, {OUT=>''}, {EXIT=>1},
+    {ERR => "$prog: '/' specifier only allowed with the last value\n"}],
+   ['e12', '-t+0 -t+5', {IN=>''}, {OUT=>''}, {EXIT=>1},
+    {ERR => "$prog: '+' specifier only allowed with the last value\n"}],
+
+   # A single zero-valued specifier is still accepted, and means
+   # "no extension", that is, a default tab size of 8.
+   ['e13', '--tabs=/0', {IN=>"a\tb"}, {OUT=>"a       b"}],
+   ['e14', '--tabs=+0', {IN=>"a\tb"}, {OUT=>"a       b"}],
+
    # A bare '-' is standard input.
    ['stdin-dash', '-', {IN_PIPE=>"stdin\n"}, {IN=>{f=>"file\n"}},
     {OUT=>"stdin\nfile\n"}],
diff --git a/tests/unexpand/unexpand.pl b/tests/unexpand/unexpand.pl
index 426612331..3403d6055 100755
--- a/tests/unexpand/unexpand.pl
+++ b/tests/unexpand/unexpand.pl
@@ -101,6 +101,14 @@ my @Tests =
      ['obs-ovflo', "-$limits->{UINTMAX_OFLOW}", {IN=>''}, {OUT=>''},
       {EXIT => 1}, {ERR => "$prog: tab stop is too large\n"}],
 
+     # A specifier with a zero value must still be counted.
+     ['spec-zero1', '--tabs=/0,5', {IN=>''}, {OUT=>''}, {EXIT => 1},
+      {ERR => "$prog: '/' specifier only allowed with the last value\n"}],
+     ['spec-zero2', '-t+0 -t+5', {IN=>''}, {OUT=>''}, {EXIT => 1},
+      {ERR => "$prog: '+' specifier only allowed with the last value\n"}],
+     ['spec-zero3', '--tabs=/0,+5', {IN=>''}, {OUT=>''}, {EXIT => 1},
+      {ERR => "$prog: '/' specifier is mutually exclusive with '+'\n"}],
+
 
      # Test input with backspaces '\b' ('bs1' is the baseline, without \b)
      # Note: If users report errors in these tests, copy&pasting results from
-- 
2.43.0




Reply via email to