path: root/player
diff options
authorwm4 <wm4@nowhere>2015-01-20 21:10:44 +0100
committerwm4 <wm4@nowhere>2015-01-20 21:10:44 +0100
commit21322a016e74ddf20cffaab5d621db1cf694eaa5 (patch)
tree9b837c6088bd758950524aa489af54c690bf6572 /player
parent9f63b9f6733869ddcd1118545d32cde850885056 (diff)
client API: check locale, and reject anything other than "C" locale
Sigh... The C locale system is incredibly shitty, and if used, breaks basic string functions. The locale can change the decimal mark from "." to ",", which affects conversion between floats and strings: snprintf() and strtod() respect the locale decimal mark, and change behavior. (What's even better, the behavior of these functions can change asynchronously, if setlocale() is called after threads were started.) So just check the locale in the client API, and refuse to work if it's wrong. This also makes the lib print to stderr, which I consider the lesser evil in this specific situation.
Diffstat (limited to 'player')
1 files changed, 16 insertions, 0 deletions
diff --git a/player/client.c b/player/client.c
index 6a21596eb3..ae4f1f3898 100644
--- a/player/client.c
+++ b/player/client.c
@@ -17,6 +17,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
+#include <locale.h>
#include <assert.h>
#include "common/common.h"
@@ -439,8 +440,23 @@ void mpv_terminate_destroy(mpv_handle *ctx)
pthread_join(playthread, NULL);
+// We mostly care about LC_NUMERIC, and how "." vs. "," is treated,
+// Other locale stuff might break too, but probably isn't too bad.
+static bool check_locale(void)
+ char *name = setlocale(LC_NUMERIC, NULL);
+ return strcmp(name, "C") == 0;
mpv_handle *mpv_create(void)
+ if (!check_locale()) {
+ // Normally, we never print anything (except if the "terminal" option
+ // is enabled), so this is an exception.
+ fprintf(stderr, "Non-C locale detected. This is not supported.\n"
+ "Call 'setlocale(LC_NUMERIC, \"C\");' in your code.\n");
+ return NULL;
+ }
struct MPContext *mpctx = mp_create();
mpv_handle *ctx = mp_new_client(mpctx->clients, "main");
if (ctx) {