]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
logger: correctly format tv_usec
authorThomas Weißschuh <thomas@t-8ch.de>
Tue, 9 Apr 2024 09:00:26 +0000 (11:00 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Tue, 9 Apr 2024 09:10:16 +0000 (11:10 +0200)
tv_usec is an unspecified signed integer type.
The format string %u assumes an unsigned int, which is incorrect.
Especially on 32bit big-endian, where it can lead to invalid values.

Reported-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Link: https://lore.kernel.org/util-linux/afef1b770ad80d50660bb2c53a0a8330b88d1049.camel@physik.fu-berlin.de/
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
misc-utils/logger.c

index ec1fc8e3479f843f9f30149f2e7e021118b0e399..f696287d7a6b8b21c57cc1afe42962a70913e0c7 100644 (file)
@@ -815,12 +815,12 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
                if (localtime_r(&tv.tv_sec, &tm) != NULL) {
                        char fmt[64];
                        const size_t i = strftime(fmt, sizeof(fmt),
-                                                 "%Y-%m-%dT%H:%M:%S.%%06u%z ", &tm);
+                                                 "%Y-%m-%dT%H:%M:%S.%%06jd%z ", &tm);
                        /* patch TZ info to comply with RFC3339 (we left SP at end) */
                        fmt[i - 1] = fmt[i - 2];
                        fmt[i - 2] = fmt[i - 3];
                        fmt[i - 3] = ':';
-                       xasprintf(&time, fmt, tv.tv_usec);
+                       xasprintf(&time, fmt, (intmax_t) tv.tv_usec);
                } else
                        err(EXIT_FAILURE, _("localtime() failed"));
        } else