[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 16:38:54 CEST 2014


Your observations are correct, and I have tried to come up with a better
version of the patch. But probably there are still issues which I am unable
to detect because of my lack of experience.
--

If the newlocale function fails and returns (locale_t)0,  subsequent calls
to
strerror_l and freelocale have an undefined behavior. In this case,
construct
a special error message in a separate buffer, indicating that there was a
problem with the locale name and getting the error message using strerror_r.
---
 src/posix/error.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/src/posix/error.c b/src/posix/error.c
index acb42c9..998cd34 100644
--- a/src/posix/error.c
+++ b/src/posix/error.c
@@ -30,6 +30,35 @@
 static const char *vlc_strerror_l(int errnum, const char *lname)
 {
     locale_t loc = newlocale(LC_MESSAGES_MASK, lname, (locale_t)0);
+
+    // Special handling in case of newlocale function failure
+    if (!loc)
+    {
+#define ERR_MSG_SIZE 0x40
+        static char err_msg_buf[ERR_MSG_SIZE];
+        static char special_err_msg[ERR_MSG_SIZE];
+        char *err_msg;
+
+#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
+        // XSI-compliant variant of strerror_r
+        strerror_r(errnum, err_msg_buf, ERR_MSG_SIZE);
+        err_msg = err_msg_buf;
+#else
+        // GNU-specific variant of strerror_r
+        err_msg = strerror_r(errnum, err_msg_buf, ERR_MSG_SIZE);
+#endif
+
+        if (strlen(lname))
+            snprintf(special_err_msg, ERR_MSG_SIZE,
+                    "[bad locale %s for error msg] %s", lname, err_msg);
+        else
+            snprintf(special_err_msg, ERR_MSG_SIZE,
+                    "[bad default locale for error msg] %s", err_msg);
+
+        return special_err_msg;
+#undef ERR_MSG_SIZE
+    }
+
     const char *buf = strerror_l(errnum, loc);

     freelocale(loc);
-- 
2.0.0



2014-07-07 12:24 GMT+03:00 Rémi Denis-Courmont <remi at remlab.net>:

> Le 2014-07-07 12:05, Casian Andrei a écrit :
>
>  If the current locale settings on a system are somehow broken, the
>> newlocale function can fail and return a null locale object.
>>
>
> Sorry but that is too fuzzy a problem description in my opinion.
>
>
>  According to the documentation, subsequent calls to strerror_l
>> and freelocale have an undefined behavior.
>>
>
> True but (vlc_)strerror_l() cannot return NULL ever anyway. So the patch
> is buggy.
>
>
>  When newlocale fails, return NULL, and cover this case in
>> vlc_strerror, where vlc_error_c can be used as a backup.
>>
>
> I find addressing a failure of newlocale() with another call to
> newlocale() dubious.
>
> --
> Rémi Denis-Courmont
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20140707/6459503a/attachment.html>


More information about the vlc-devel mailing list