]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
dd: do not access uninitialized
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 31 Jan 2022 18:20:21 +0000 (10:20 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 31 Jan 2022 20:07:39 +0000 (12:07 -0800)
* src/dd.c (parse_integer): Avoid undefined behavior
that accesses an uninitialized ā€˜n’ when e == LONGINT_INVALID.
Return more-accurate error code when INTMAX_MAX < n.

src/dd.c

index e55f87f1496c377ee0911cf970b3dee2105be05c..7360a497387f43a43a9f6cddce33a3fea4f6cfe0 100644 (file)
--- a/src/dd.c
+++ b/src/dd.c
@@ -1427,8 +1427,10 @@ static intmax_t
 parse_integer (char const *str, strtol_error *invalid)
 {
   /* Call xstrtoumax, not xstrtoimax, since we don't want to
-     allow strings like "  -0".  */
-  uintmax_t n;
+     allow strings like " -0".  Initialize N to an interminate value;
+     calling code should not rely on this function returning 0
+     when *INVALID represents a non-overflow error.  */
+  uintmax_t n = 0;
   char *suffix;
   strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
 
@@ -1468,7 +1470,7 @@ parse_integer (char const *str, strtol_error *invalid)
 
   if (INTMAX_MAX < n)
     {
-      *invalid = LONGINT_OVERFLOW;
+      *invalid = e | LONGINT_OVERFLOW;
       return INTMAX_MAX;
     }