diff options
author | Thomas Weißschuh <thomas@t-8ch.de> | 2024-04-09 11:00:08 +0200 |
---|---|---|
committer | Thomas Weißschuh <thomas@t-8ch.de> | 2024-04-09 11:10:11 +0200 |
commit | 564750580b2a78c2f3f0e8d02bdef9503d6b110c (patch) | |
tree | a9c9bc0bebf4d732a7fcda500f20fe18e74be456 | |
parent | 1c0137735020c36ce8fb39d731b2acef7e4cb0cd (diff) | |
download | util-linux-564750580b2a78c2f3f0e8d02bdef9503d6b110c.tar.gz |
logger: rework error handling in logger_gettimeofday()
* 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 <thomas@t-8ch.de>
-rw-r--r-- | misc-utils/logger.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/misc-utils/logger.c b/misc-utils/logger.c index b4a909438b..ec1fc8e347 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) |