libdlog doesn't modify errno from outside PoV 36/218136/1
authorMichal Bloch <m.bloch@samsung.com>
Tue, 19 Nov 2019 10:22:22 +0000 (11:22 +0100)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 20 Nov 2019 01:01:39 +0000 (01:01 +0000)
Change-Id: If5b0fabec233d805041fa534933828cc72bf87cf
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
(cherry picked from commit 6afdbf69c184f8b1b466af16d65272ad9e7f757a)

src/libdlog/log.c

index 755429e..06b4207 100644 (file)
@@ -310,6 +310,19 @@ static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char
        if (ret < 0)
                return ret;
 
+       /* DLog is often used to log errors, but people usually fail to
+        * handle errno correctly, forgetting that logging can also
+        * modify it, like this:
+        *
+        * if (foo() == -1) {
+        *     dlog_print(...);
+        *     if (errno == ...) // potentially uses DLog's errno
+        *
+        * Therefore we take an extra step to remedy this. It is fairly
+        * unfortunate to have to do this because it might encourage lazy
+        * coding, but it's probably more important that the system works. */
+       const int initial_errno = errno;
+
        /* Threads can be cancelled before they give up a lock.
         * Therefore cancellation is temporarily disabled.
         * This solution is comparatively simple and cheap.
@@ -334,6 +347,7 @@ static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char
 
        pthread_setcancelstate(old_cancel_state, NULL);
 
+       errno = initial_errno;
        return ret;
 }