Subject: [PATCH] shuf: fix bug with "-i 1-0"

From: Erik Auerswald <[email-masked]>
Date: Fri, 25 Jun 2021 15:23:33 +0200

"shuf -i 1-0" would mistakenly accept the invalid range
without an error message and produce no output.  Other
invalid ranges, e.g., "shuf -i 2-0", would be detected
and produce an error message, non-zero exit code, and
no output.

Bug reported by "F8ER F8ER."

* src/shuf.c (main): Fix bug.
* tests/misc/shuf.sh: Add a test case for the bug.
---
 src/shuf.c         | 2 +-
 tests/misc/shuf.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/shuf.c b/src/shuf.c
index 1af1b533a..91430a88a 100644
--- a/src/shuf.c
+++ b/src/shuf.c
@@ -431,7 +431,7 @@ main (int argc, char **argv)
                                  _("invalid input range"), 0);
 
           n_lines = hi_input - lo_input + 1;
-          invalid |= ((lo_input <= hi_input) == (n_lines == 0));
+          invalid |= (lo_input > hi_input);
           if (invalid)
             die (EXIT_FAILURE, errno, "%s: %s", _("invalid input range"),
                  quote (optarg));
diff --git a/tests/misc/shuf.sh b/tests/misc/shuf.sh
index 892386b3f..2a7cba4d3 100755
--- a/tests/misc/shuf.sh
+++ b/tests/misc/shuf.sh
@@ -95,7 +95,7 @@ test "$c" -eq 3 || { fail=1; echo "Multiple -n failed">&2 ; }
 { shuf -i0-9 -n10 -i8-90 || test $? -ne 1; } &&
   { fail=1; echo "shuf did not detect multiple -i usage.">&2 ; }
 # Test invalid range
-for ARG in '1' 'A' '1-' '1-A'; do
+for ARG in '1' 'A' '1-' '1-A' '1-0' '2-0'; do
     { shuf -i$ARG || test $? -ne 1; } &&
     { fail=1; echo "shuf did not detect erroneous -i$ARG usage.">&2 ; }
 done
-- 
2.17.1