]> git.sesse.net Git - vlc/blobdiff - bin/override.c
decoder: fix data race in input_DecoderChangePause()
[vlc] / bin / override.c
index 5996e43c499b44067a2b67b8d4cdbf1840062dc1..fb4608c969c8d9c16f255a17180a6cade4e9587b 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * override.c: overriden function calls for VLC media player
+ * override.c: overridden function calls for VLC media player
  *****************************************************************************
  * Copyright (C) 2010 RĂ©mi Denis-Courmont
  *
@@ -23,6 +23,7 @@
 #endif
 
 #include <stdbool.h>
+#define MAX_ERRORS 5
 
 void vlc_enable_override (void);
 
@@ -33,10 +34,12 @@ void vlc_enable_override (void);
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <dlfcn.h>
 #include <pthread.h>
 #ifdef HAVE_EXECINFO_H
 # include <execinfo.h>
+# include <unistd.h>
 #endif
 #ifdef NDEBUG
 # undef HAVE_BACKTRACE
@@ -55,32 +58,37 @@ void vlc_enable_override (void)
     pthread_atfork (NULL, NULL, vlc_reset_override);
 }
 
-static void vlogbug (const char *level, const char *func, const char *fmt,
-                     va_list ap)
+static void vlogbug (unsigned *pc, const char *level, const char *func,
+                     const char *fmt, va_list ap)
 {
 #ifdef HAVE_BACKTRACE
-    const size_t framec = 4;
+    const size_t framec = 5;
     void *framev[framec];
 
     backtrace (framev, framec);
 #endif
     flockfile (stderr);
-    fprintf (stderr, "%s: call to %s(", level, func);
-    vfprintf (stderr, fmt, ap);
-    fputs (")\n", stderr);
-    fflush (stderr);
+    if (*pc < MAX_ERRORS)
+    {
+        (*pc)++;
+        fprintf (stderr, "%s: call to %s(", level, func);
+        vfprintf (stderr, fmt, ap);
+        fputs (")\n", stderr);
+        fflush (stderr);
 #ifdef HAVE_BACKTRACE
-    backtrace_symbols_fd (framev + 2, framec - 2, fileno (stderr));
+        backtrace_symbols_fd (framev + 2, framec - 2, STDERR_FILENO);
 #endif
+    }
     funlockfile (stderr);
 }
 
-static void logbug (const char *level, const char *func, const char *fmt, ...)
+static void logbug (unsigned *pc, const char *level, const char *func,
+                    const char *fmt, ...)
 {
     va_list ap;
 
     va_start (ap, fmt);
-    vlogbug (level, func, fmt, ap);
+    vlogbug (pc, level, func, fmt, ap);
     va_end (ap);
 }
 
@@ -96,25 +104,18 @@ static void *getsym (const char *name)
     return sym;
 }
 
-#define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
+#define LOG(level, ...) \
+    do { \
+        static unsigned counter = 0; \
+        logbug(&counter, level, __func__, __VA_ARGS__); \
+    } while (0)
+
 /* Evil non-standard GNU C macro ;)
  *  typeof keyword,
- *  statement-expression,
- *  nested function...
+ *  statement-expression
  */
 #define CALL(func, ...) \
-({ \
-    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__); \
-})
-
+({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
 
 /*** Environment ***
  *
@@ -189,6 +190,7 @@ int rand (void)
 }
 
 
+#if 0
 /** Signals **/
 #include <signal.h>
 
@@ -216,7 +218,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:
@@ -231,13 +234,15 @@ 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:
     LOG("Blocked", "%d, %p, %p", signum, act, old);
     return -1;
 }
+#endif
 
 
 /*** Locales ***
@@ -258,6 +263,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>