From: Paul Eggert Date: Tue, 19 Apr 2022 23:13:55 +0000 (-0700) Subject: pr: don’t use uninitialized var X-Git-Tag: v9.2~203 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=81d58df1647ea79c5161f99d8bd241f0c78df729;p=thirdparty%2Fcoreutils.git pr: don’t use uninitialized var 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. --- diff --git a/src/pr.c b/src/pr.c index 4c17c00505..a8feba9d8f 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);