]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
logger: rework error handling in logger_gettimeofday()
authorThomas Weißschuh <thomas@t-8ch.de>
Tue, 9 Apr 2024 09:00:08 +0000 (11:00 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Tue, 9 Apr 2024 09:10:11 +0000 (11:10 +0200)
* 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>
misc-utils/logger.c

index b4a909438b30763e1f15133d2b7e3b01ac98f296..ec1fc8e3479f843f9f30149f2e7e021118b0e399 100644 (file)
@@ -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)