[vlc-devel] [PATCH] error.c: cover the case when newlocale fails and returns a null locale object

Casian Andrei skeletk13 at gmail.com
Mon Jul 7 11:05:45 CEST 2014


If the current locale settings on a system are somehow broken, the
newlocale function can fail and return a null locale object. According
to the documentation, subsequent calls to strerror_l and freelocale have
an undefined behavior. This may be a segmentation fault.

When newlocale fails, return NULL, and cover this case in vlc_strerror,
where vlc_error_c can be used as a backup.
---
 src/posix/error.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/posix/error.c b/src/posix/error.c
index acb42c9..b270e6e 100644
--- a/src/posix/error.c
+++ b/src/posix/error.c
@@ -30,6 +30,9 @@
 static const char *vlc_strerror_l(int errnum, const char *lname)
 {
     locale_t loc = newlocale(LC_MESSAGES_MASK, lname, (locale_t)0);
+    if (!loc)
+        return NULL;
+
     const char *buf = strerror_l(errnum, loc);

     freelocale(loc);
@@ -38,6 +41,8 @@ static const char *vlc_strerror_l(int errnum, const char
*lname)

 /**
  * Formats an error message in the current locale.
+ * If there is an issue with the current locale, then @ref vlc_strerror_c
+ * is used instead.
  * @param errnum error number (as in errno.h)
  * @return A string pointer, valid until the next call to a function of the
  * strerror() family in the same thread. This function cannot fail.
@@ -45,7 +50,10 @@ static const char *vlc_strerror_l(int errnum, const char
*lname)
 const char *vlc_strerror(int errnum)
 {
     /* We cannot simply use strerror() here, since it is not thread-safe.
*/
-    return vlc_strerror_l(errnum, "");
+    const char *buf = vlc_strerror_l(errnum, "");
+    if (!buf)
+        return vlc_strerror_c(errnum);
+    return buf;
 }

 /**
-- 
2.0.0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20140707/eebb428b/attachment.html>


More information about the vlc-devel mailing list