]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
test: handle adjacent binary operators
authorCollin Funk <collin.funk1@gmail.com>
Fri, 24 Jul 2026 04:12:42 +0000 (21:12 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Sun, 26 Jul 2026 19:44:01 +0000 (12:44 -0700)
* NEWS: Mention the bug fix.
* cfg.mk (exclude_file_name_regexp--sc_prohibit_test_minus_ao): Exclude
NEWS.
* src/test.c (term): Treat '-a' and '-o' as strings instead of expecting
them to be valid unary operators.
* tests/test/test.pl: Add some test cases.
* tests/test/test-diag.pl: Also test the diagnostics with '-a arg'.

NEWS
cfg.mk
src/test.c
tests/test/test-diag.pl
tests/test/test.pl

diff --git a/NEWS b/NEWS
index d47d9517e2cc5345abb2ff3c9c99965271d5584b..fd0902cb724a1bd97b4f705878abf604cf696607 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -44,6 +44,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   'tee' no longer treats short writes as errors.
   [bug introduced in coreutils-9.11]
 
+  'test' no longer treats '-a' and '-o' as operators when given as strings to a
+  binary operator.  E.g., 'test -a -a -a' exits successfully instead of exiting
+  with an error.
+  [This bug was present in "the beginning".]
+
   'unexpand -t' no longer overflows a heap buffer, for tab values > SIZE_MAX/16,
   or with multi-byte blank characters longer than the tab value.
   [bugs introduced in coreutils-9.11]
diff --git a/cfg.mk b/cfg.mk
index 90e6a21d115308b096417da20220f6129ba0ce40..b21b7dc0393181178f8186100a46c3cef488a66a 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -950,7 +950,7 @@ exclude_file_name_regexp--sc_prohibit_always_true_header_tests = \
   ^m4/stat-prog\.m4$$
 exclude_file_name_regexp--sc_prohibit_fail_0 = \
   (^.*/git-hooks/commit-msg|^tests/init\.sh|Makefile\.am|\.mk|.*\.texi)$$
-exclude_file_name_regexp--sc_prohibit_test_minus_ao = doc/.*\.texi$$
+exclude_file_name_regexp--sc_prohibit_test_minus_ao = NEWS|doc/.*\.texi$$
 exclude_file_name_regexp--sc_prohibit_atoi_atof = ^lib/euidaccess-stat\.c$$
 
 # longlong.h is maintained elsewhere.
index 692c37263b7b5ed1d070bb6695d22fdb793d1377..f28fb7c9de9eb23978e0262700a06d7b39cc830b 100644 (file)
@@ -265,7 +265,8 @@ term (void)
     value = binary_operator (false, bop);
 
   /* It might be a switch type argument.  */
-  else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][2] == '\0')
+  else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][1] != 'a'
+           && argv[pos][1] != 'o' && argv[pos][2] == '\0')
     value = unary_operator ();
   else
     {
index b9929d9c5c26c945df920cb70724af02876c5f16..8a25570453facdbd1a72e4012f4297eacae9d0fd 100755 (executable)
@@ -26,9 +26,8 @@ use strict;
 my @Tests =
     (
      # In coreutils-5.93, this diagnostic lacked the newline.
-     ['o', '-o arg', {ERR => "test: '-o': unary operator expected\n"},
-      {ERR_SUBST => 's!^.*test:!test:!'},
-      {EXIT => 2}],
+     map {[$_, "-$_ arg", {ERR => "test: '-$_': unary operator expected\n"},
+           {ERR_SUBST => 's!^.*test:!test:!'}, {EXIT => 2}]} qw(o a),
     );
 
 my $save_temps = $ENV{DEBUG};
index 0f0a93d67b7f6aa4683335d4a3beefe8f7ca25a6..9b722191df6e9c9400fc629388640558dedc4030 100755 (executable)
@@ -204,6 +204,24 @@ my @Tests =
   ['greater-collate-3', "'a' '>' 'b'", {EXIT=>1}],
 );
 
+# Test the behavior of multiple adjacent binary operators.
+# These would not behave correctly in coreutils-9.11 and earlier.
+my $i = 1;
+my @operands = ('-a', '-o', "''");
+for my $b ('-a', '-o')
+  {
+    for my $a (@operands)
+      {
+        for my $c (@operands)
+          {
+            push @Tests, ["multiple-binary-" . ++$i, "$a $b $c",
+                          {EXIT=>0 + (($b eq '-o')
+                                      ? ($a eq "''" && $c eq "''")
+                                      : ($a eq "''" || $c eq "''"))}];
+          }
+      }
+  }
+
 @Tests = add_inverse_op_tests \@Tests;
 @Tests = add_pn_tests \@Tests;