From 81d58df1647ea79c5161f99d8bd241f0c78df729 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 19 Apr 2022 16:13:55 -0700
Subject: [PATCH] =?UTF-8?q?pr:=20don=E2=80=99t=20use=20uninitialized=20var?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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.

Reference: https://github.com/coreutils/coreutils/commit/81d58df1647ea79c5161f99d8bd241f0c78df729
Conflict: NA

---
 src/pr.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/pr.c b/src/pr.c
index 4c17c0050..a8feba9d8 100644
--- 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);
-- 
2.27.0

