From 36fac0849ceabafb9e2a15045230833e7dbc9e9d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 28 Aug 2015 15:38:04 -0400 Subject: [PATCH] Make g_strerror threadsafe We need to use strerror_r here, in order to be threadsafe. --- glib/gstrfuncs.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 6c079bfe8..d8c5e4260 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -1243,23 +1243,35 @@ g_ascii_strtoll (const gchar *nptr, * strerror(), because it returns a string in UTF-8 encoding, and since * not all platforms support the strerror() function. * + * Note that the string may be translated according to the current locale. + * * Returns: a UTF-8 string describing the error code. If the error code - * is unknown, it returns "unknown error ()". + * is unknown, it returns a string like "unknown error ()". */ const gchar * g_strerror (gint errnum) { + gchar buf[1024]; gchar *msg; gchar *tofree = NULL; const gchar *ret; gint saved_errno = errno; + GError *error = NULL; - msg = strerror (errnum); + /* Since we are building with _GNU_SOURCE, we get the + * GNU variant of strerror_r (with glibc). + */ + msg = strerror_r (errnum, buf, sizeof (buf)); if (!g_get_charset (NULL)) - msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL); + { + msg = tofree = g_locale_to_utf8 (msg, -1, NULL, NULL, &error); + if (error) + g_print ("%s\n", error->message); + } ret = g_intern_string (msg); g_free (tofree); + errno = saved_errno; return ret; }