Fix: logging: log_add_time() save/restore errno
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 2 Apr 2019 17:41:17 +0000 (13:41 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 24 Apr 2019 22:58:44 +0000 (18:58 -0400)
The debugging logging macros (e.g. DBG()) are used as printf in the
lttng-tools source files. The printf() implementation does not alter the
errno value, so the fact that log_add_time() (through clock_gettime())
can alter errno is unexpected. For instance, adding a logging statement
for debugging purposes within a function for which errno is expected to
stay unchanged on return will change the behavior between execution with
-vvv and non-verbose.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/error.c

index 236f21baee25fea33a1d9b4b35ba76796bbf3832..a7d6e1220a88be351397ce65bc919f16f2266234 100644 (file)
@@ -20,6 +20,7 @@
 #include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #include <lttng/lttng-error.h>
 #include <common/common.h>
@@ -45,6 +46,7 @@ const char *log_add_time(void)
        struct tm tm, *res;
        struct timespec tp;
        time_t now;
+       const int errsv = errno;
 
        ret = lttng_clock_gettime(CLOCK_REALTIME, &tp);
        if (ret < 0) {
@@ -65,10 +67,12 @@ const char *log_add_time(void)
                goto error;
        }
 
+       errno = errsv;
        return URCU_TLS(error_log_time).str;
 
 error:
        /* Return an empty string on error so logging is not affected. */
+       errno = errsv;
        return "";
 }
 
This page took 0.027278 seconds and 5 git commands to generate.