[vlc-commits] posix: handle newlocale() errors
    Rémi Denis-Courmont 
    git at videolan.org
       
    Mon Jul  7 19:39:17 CEST 2014
    
    
  
vlc/vlc-2.2 | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Mon Jul  7 20:25:09 2014 +0300| [464281390a6f9db18393bbd27729f5efba7ea31e] | committer: Rémi Denis-Courmont
posix: handle newlocale() errors
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 at gmail.com>
(cherry picked from commit d0822ab760020a2ff371a3a99ab3ef29d4271c06)
> http://git.videolan.org/gitweb.cgi/vlc/vlc-2.2.git/?a=commit;h=464281390a6f9db18393bbd27729f5efba7ea31e
---
 src/posix/error.c |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
diff --git a/src/posix/error.c b/src/posix/error.c
index acb42c9..db51004 100644
--- a/src/posix/error.c
+++ b/src/posix/error.c
@@ -23,13 +23,31 @@
 #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);
    
    
More information about the vlc-commits
mailing list