]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
od: more minor fixes for offsets
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 30 Jun 2025 05:25:28 +0000 (22:25 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 30 Jun 2025 05:26:14 +0000 (22:26 -0700)
* src/od.c (parse_old_offset): Reject invalid offsets like "++0".
Treat overflowing offsets as errors, not as file names.

src/od.c

index 8acf205e1ddbf741a2186368a50f2cfb0e984f17..918d56725ad2652b5e1c2ab6ed628e2916bafc2e 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -1409,18 +1409,20 @@ xstr2nonneg (char const *restrict nptr, int base, intmax_t *val,
   return s_err != LONGINT_INVALID && *val < 0 ? LONGINT_INVALID : s_err;
 }
 
-/* If S is a valid traditional offset specification with an optional
-   leading '+' return true and set *OFFSET to the offset it denotes.
-   Otherwise return false and possibly set *OFFSET.  */
+/* If STR is a valid traditional offset specification with an optional
+   leading '+', and can be represented in intmax_t, return true and
+   set *OFFSET to the offset it denotes.  If it is valid but cannot be
+   represented, diagnose the problem and exit.  Otherwise return false
+   and possibly set *OFFSET.  */
 
 static bool
-parse_old_offset (char *s, intmax_t *offset)
+parse_old_offset (char *str, intmax_t *offset)
 {
-  if (*s == '\0')
-    return false;
-
   /* Skip over any leading '+'.  */
-  s += s[0] == '+';
+  char *s = str + (str[0] == '+');
+
+  if (! c_isdigit (s[0]))
+    return false;
 
   /* Determine the radix for S.  If there is a '.', followed first by
      optional 'b' or (undocumented) 'B' and then by end of string,
@@ -1447,6 +1449,8 @@ parse_old_offset (char *s, intmax_t *offset)
       dot[0] = '.';
     }
 
+  if (s_err == LONGINT_OVERFLOW)
+    error (EXIT_FAILURE, ERANGE, "%s", quotearg_colon (str));
   return s_err == LONGINT_OK;
 }