]> git.sesse.net Git - vlc/blobdiff - bin/override.c
Catch unsafe (but alas common) strerror()
[vlc] / bin / override.c
index 02402d50ec554d34e9b795237096d26b967a49a0..b5de22d0f558b4c941937048c63ea11c75ffc0a9 100644 (file)
 
 void vlc_enable_override (void);
 
-#if defined (__GNUC__) /* typeof and statement-expression */ \
+#if defined (__GNUC__) \
  && (defined (__ELF__) && !defined (__sun__))
 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
 
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <dlfcn.h>
 #include <pthread.h>
 #ifdef HAVE_EXECINFO_H
@@ -97,8 +98,23 @@ static void *getsym (const char *name)
 }
 
 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
+/* Evil non-standard GNU C macro ;)
+ *  typeof keyword,
+ *  statement-expression,
+ *  nested function...
+ */
 #define CALL(func, ...) \
-    ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
+({ \
+    static typeof (func) *sym = NULL; \
+    static pthread_once_t once = PTHREAD_ONCE_INIT; \
+    auto void getsym_once (void); \
+    void getsym_once (void) \
+    { \
+        sym = getsym ( # func); \
+    } \
+    pthread_once (&once, getsym_once); \
+    sym (__VA_ARGS__); \
+})
 
 
 /*** Environment ***
@@ -201,7 +217,8 @@ void (*signal (int signum, void (*handler) (int))) (int)
         if (!blocked_signal (signum))
             goto error;
         /* For our blocked signals, the handler won't matter much... */
-        LOG("Warning", "%d, %p", signum, handler);
+        if (handler == SIG_DFL)
+            LOG("Warning", "%d, SIG_DFL", signum, handler);
     }
     return CALL(signal, signum, handler);
 error:
@@ -216,7 +233,8 @@ int sigaction (int signum, const struct sigaction *act, struct sigaction *old)
         if ((act->sa_flags & SA_SIGINFO)
          || (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL))
             goto error;
-        LOG("Warning", "%d, %p, %p", signum, act, old);
+        if (act->sa_handler == SIG_DFL)
+            LOG("Warning", "%d, %p, SIG_DFL", signum, act);
     }
     return CALL(sigaction, signum, act, old);
 error:
@@ -243,6 +261,20 @@ char *setlocale (int cat, const char *locale)
 }
 
 
+/* strerror() is not thread-safe in theory (POSIX), nor in practice (glibc).
+ * This caused quite nasty crashes in the history of VLC/Linux. */
+char *strerror (int val)
+{
+    if (override)
+    {
+        static const char msg[] =
+            "Error message unavailable (use strerror_r instead of strerror)!";
+        LOG("Blocked", "%d", val);
+        return (char *)msg;
+    }
+    return CALL(strerror, val);
+}
+
 /*** Xlib ****/
 #ifdef HAVE_X11_XLIB_H
 # include <X11/Xlib.h>