]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
pr: don’t use uninitialized var
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 19 Apr 2022 23:13:55 +0000 (16:13 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 19 Apr 2022 23:14:50 +0000 (16:14 -0700)
Found with -flto and --enable-gcc-warnings.
* src/pr.c (getoptarg): Fix misuse of xstrtol, which does not
necessarily set tmp_long on errror, and does not set errno in any
reliable way.  The previous code might access uninitialized
storage; on typical platforms this merely causes it to possibly
print the wrong diagnostic.

src/pr.c

index 4c17c0050536c170c3cd1d8cd27622ef7945b233..a8feba9d8fbb5c864b7971b48f0ac7d0df53b293 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
@@ -1173,10 +1173,17 @@ getoptarg (char *arg, char switch_char, char *character, int *number)
   if (*arg)
     {
       long int tmp_long;
-      if (xstrtol (arg, NULL, 10, &tmp_long, "") != LONGINT_OK
-          || tmp_long <= 0 || INT_MAX < tmp_long)
+      strtol_error e = xstrtol (arg, NULL, 10, &tmp_long, "");
+      if (e == LONGINT_OK)
         {
-          error (0, INT_MAX < tmp_long ?  EOVERFLOW : errno,
+          if (tmp_long <= 0)
+            e = LONGINT_INVALID;
+          else if (INT_MAX < tmp_long)
+            e = LONGINT_OVERFLOW;
+        }
+      if (e != LONGINT_OK)
+        {
+          error (0, e & LONGINT_OVERFLOW ? EOVERFLOW : 0,
              _("'-%c' extra characters or invalid number in the argument: %s"),
                  switch_char, quote (arg));
           usage (EXIT_FAILURE);