]> git.sesse.net Git - vlc/commitdiff
posix: handle newlocale() errors
authorRémi Denis-Courmont <remi@remlab.net>
Mon, 7 Jul 2014 17:25:09 +0000 (20:25 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Mon, 7 Jul 2014 17:26:46 +0000 (20:26 +0300)
newlocale() will fail if the specified locale is not found. Here, it
can fail if the environment variables refer to an invalid or missing
locale.

Pointed-out-by: Casian Andrei <skeletk13@gmail.com>
src/posix/error.c

index acb42c91824a602a7b1e9e3f79743dcab3f8e80a..db51004601906825dd15654838909a7cf45013b3 100644 (file)
 #endif
 
 #include <string.h>
+#include <errno.h>
 #include <locale.h>
+#include <assert.h>
 
 #include <vlc_common.h>
 
 static const char *vlc_strerror_l(int errnum, const char *lname)
 {
+    int saved_errno = errno;
     locale_t loc = newlocale(LC_MESSAGES_MASK, lname, (locale_t)0);
+
+    if (unlikely(loc == (locale_t)0))
+    {
+        if (errno == ENOENT) /* fallback to POSIX locale */
+            loc = newlocale(LC_MESSAGES_MASK, "C", (locale_t)0);
+
+        if (unlikely(loc == (locale_t)0))
+        {
+            assert(errno != EINVAL && errno != ENOENT);
+            errno = saved_errno;
+            return "Error message unavailable";
+        }
+        errno = saved_errno;
+    }
+
     const char *buf = strerror_l(errnum, loc);
 
     freelocale(loc);