From 604f8a6c4d58a646c8722fdf7cad9ee67479d8f7 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 31 Jan 2022 10:20:21 -0800 Subject: [PATCH] dd: do not access uninitialized MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/dd.c b/src/dd.c index e55f87f149..7360a49738 100644 --- 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; } -- 2.47.2