1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-07-26 10:39:01 +02:00

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.
This commit is contained in:
Paul Eggert
2022-04-19 16:13:55 -07:00
parent 913f14bbd0
commit 81d58df164
+10 -3
View File
@@ -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);