From: Ismail Ramzi <[email protected]>

You can repro this with: seq 1e-4294967301 1

* src/seq.c (scan_arg): Saturate the precision derived from a negative
exponent at INT_MAX, rather than letting it wrap around the int, which
also caused signed overflow in get_default_format with -w.
* tests/seq/seq-precision.sh: Add test cases.
* NEWS: Mention the bug fix.

Link: https://github.com/coreutils/coreutils/pull/470
---
 NEWS                       | 5 +++++
 src/seq.c                  | 6 ++++--
 tests/seq/seq-precision.sh | 7 +++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 00cf3d830..6d283b4b5 100644
--- a/NEWS
+++ b/NEWS
@@ -269,6 +269,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   Also negative operands like "-,5" are no longer treated as options.
   [bugs introduced in coreutils-8.24 and coreutils-8.31]
 
+  'seq' now uses its general output format when a negative decimal exponent
+  would make the precision exceed INT_MAX.  Previously this could produce
+  an incorrect precision or cause integer overflow with -w.
+  [bug introduced in coreutils-6.10]
+
   'shred --force' no longer has races when opening non-writable files.
   Previously, upon failing to open a writable file descriptor, it would
   change its permissions before reopening it, following symbolic links in the
diff --git a/src/seq.c b/src/seq.c
index 961e057eb..1483b87cd 100644
--- a/src/seq.c
+++ b/src/seq.c
@@ -219,8 +219,10 @@ scan_arg (char const *arg)
       if (e)
         {
           long exponent = MAX (strtol (e + 1, NULL, 10), -LONG_MAX);
-          ret.precision += exponent < 0 ? -exponent
-                                        : - MIN (ret.precision, exponent);
+          /* Saturate at INT_MAX, i.e., treat the precision as unknown.  */
+          ret.precision += (exponent < 0
+                            ? MIN (-exponent, INT_MAX - ret.precision)
+                            : - MIN (ret.precision, exponent));
           /* Don't account for e.... in the width since this is not output.  */
           ret.width -= strlen (arg) - (e - arg);
           /* Adjust the width as per the exponent.  */
diff --git a/tests/seq/seq-precision.sh b/tests/seq/seq-precision.sh
index 5daf964fa..fffc572d8 100755
--- a/tests/seq/seq-precision.sh
+++ b/tests/seq/seq-precision.sh
@@ -82,6 +82,13 @@ compare exp out || fail=1
 seq 1e$LONG_MIN 2> err || fail=1
 compare /dev/null err || fail=1
 
+# The precision implied by an INT_MIN exponent exceeds INT_MAX.
+# Use -w to exercise the width calculation that could overflow.
+seq -w 1e$INT_MIN 2 > out 2> err || fail=1
+printf '%s\n' 0 1 2 > exp || framework_failure_
+compare exp out || fail=1
+compare /dev/null err || fail=1
+
 # Ensure a large format precision is fully honored
 seq -f '%.70000f' 1 > out || fail=1
 test "$(wc -c < out)" = 70003 || fail=1
-- 
2.56.0


Reply via email to