]> granicus.if.org Git - p11-kit/commitdiff
debug: Add p11_debug_err to prevent use of strerror
authorDaiki Ueno <dueno@redhat.com>
Mon, 12 Jun 2017 09:09:43 +0000 (11:09 +0200)
committerDaiki Ueno <ueno@gnu.org>
Mon, 12 Jun 2017 09:30:42 +0000 (11:30 +0200)
common/debug.c
common/debug.h
p11-kit/rpc-transport.c

index 47933fa8af1a01a6bdd10116a5b9dd7c1e90292e..5f7546e3aeff09ac06528fa4a567653b556dc5fc 100644 (file)
 #include "debug.h"
 
 #include <assert.h>
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif
 #include <stdio.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+#define P11_DEBUG_MESSAGE_MAX 512
+
 struct DebugKey {
        const char *name;
        int value;
@@ -139,6 +144,36 @@ p11_debug_message (int flag,
        }
 }
 
+void
+p11_debug_message_err (int flag,
+                      int errnum,
+                      const char *format, ...)
+{
+       va_list args;
+       char strerr[P11_DEBUG_MESSAGE_MAX];
+#ifdef HAVE_STRERROR_L
+       locale_t loc;
+#endif
+
+       if (flag & p11_debug_current_flags) {
+               fprintf (stderr, "(p11-kit:%d) ", getpid());
+               va_start (args, format);
+               vfprintf (stderr, format, args);
+               va_end (args);
+
+               snprintf (strerr, sizeof (strerr), "Unknown error %d", errnum);
+#ifdef HAVE_STRERROR_L
+               loc = uselocale ((locale_t) 0);
+               if (loc != NULL)
+                       strncpy (strerr, strerror_l (errnum, loc), sizeof (strerr));
+#else
+               strerror_r (errnum, strerr, sizeof (strerr));
+#endif
+               strerr[P11_DEBUG_MESSAGE_MAX - 1] = 0;
+               fprintf (stderr, ": %s\n", strerr);
+       }
+}
+
 void
 p11_debug_precond (const char *format,
                     ...)
index 6106f19e6df3a8e26ede239167191ae1c01b6770..4bf7e7866239a989fd5bf52ed0377c3a86dfd409 100644 (file)
@@ -56,6 +56,11 @@ void              p11_debug_message             (int flag,
                                                  const char *format,
                                                  ...) GNUC_PRINTF (2, 3);
 
+void              p11_debug_message_err         (int flag,
+                                                int errnum,
+                                                 const char *format,
+                                                 ...) GNUC_PRINTF (3, 4);
+
 void              p11_debug_precond             (const char *format,
                                                  ...) GNUC_PRINTF (1, 2)
                                                  CLANG_ANALYZER_NORETURN;
@@ -127,14 +132,20 @@ void              p11_debug_precond             (const char *format,
                p11_debug_message (P11_DEBUG_FLAG, "%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
        } while (0)
 
+#undef p11_debug_err
+#define p11_debug_err(errnum, format, ...) do {              \
+       if (P11_DEBUG_FLAG & p11_debug_current_flags) \
+               p11_debug_message_err (P11_DEBUG_FLAG, errnum, "%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
+       } while (0)
+
 #undef p11_debugging
 #define p11_debugging \
        (P11_DEBUG_FLAG & p11_debug_current_flags)
 
 #else /* !defined (WITH_DEBUG) */
 
-#undef p11_debug
-#define p11_debug(format, ...) \
+#undef p11_debug_err
+#define p11_debug_err(errnum, format, ...) \
        do {} while (false)
 
 #undef p11_debugging
index 9eae8834f23448c186b80b75e0b6d929c57a8c90..b46a27cca4be4b1ada0b9bc8c2f7cf352fb985ab 100644 (file)
@@ -1065,7 +1065,7 @@ rpc_unix_connect (p11_rpc_client_vtable *vtable,
        }
 
        if (connect (fd, (struct sockaddr *)&run->sa, sizeof (run->sa)) < 0) {
-               p11_debug ("failed to connect to socket: %s", strerror (errno));
+               p11_debug_err (errno, "failed to connect to socket");
                close (fd);
                return CKR_DEVICE_REMOVED;
        }