From 564750580b2a78c2f3f0e8d02bdef9503d6b110c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Tue, 9 Apr 2024 11:00:08 +0200 Subject: [PATCH] logger: rework error handling in logger_gettimeofday() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Fail when LOGGER_TEST_TIMEOFDAY is set to an invalid value * Fail with return -1 and errno, the same as normal gettimeofday() Signed-off-by: Thomas Weißschuh --- misc-utils/logger.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/misc-utils/logger.c b/misc-utils/logger.c index b4a909438..ec1fc8e34 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -154,13 +154,24 @@ static inline int logger_gettimeofday(struct timeval *tv, struct timezone *tz) char *str = getenv("LOGGER_TEST_TIMEOFDAY"); uintmax_t sec, usec; - if (str && sscanf(str, "%ju.%ju", &sec, &usec) == 2) { + if (str) { + if (sscanf(str, "%ju.%ju", &sec, &usec) != 2) + goto err; + tv->tv_sec = sec; tv->tv_usec = usec; - return tv->tv_sec >= 0 && tv->tv_usec >= 0 ? 0 : -EINVAL; + + if (tv->tv_sec >= 0 && tv->tv_usec >= 0) + return 0; + else + goto err; } return gettimeofday(tv, tz); + +err: + errno = EINVAL; + return -1; } static inline char *logger_xgethostname(void) -- 2.47.3