mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-30 15:56:18 +01:00
Make g_strerror threadsafe
We need to use strerror_r here, in order to be threadsafe.
This commit is contained in:
parent
eb7ffccf44
commit
36fac0849c
@ -1243,23 +1243,35 @@ g_ascii_strtoll (const gchar *nptr,
|
|||||||
* strerror(), because it returns a string in UTF-8 encoding, and since
|
* strerror(), because it returns a string in UTF-8 encoding, and since
|
||||||
* not all platforms support the strerror() function.
|
* 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
|
* Returns: a UTF-8 string describing the error code. If the error code
|
||||||
* is unknown, it returns "unknown error (<code>)".
|
* is unknown, it returns a string like "unknown error (<code>)".
|
||||||
*/
|
*/
|
||||||
const gchar *
|
const gchar *
|
||||||
g_strerror (gint errnum)
|
g_strerror (gint errnum)
|
||||||
{
|
{
|
||||||
|
gchar buf[1024];
|
||||||
gchar *msg;
|
gchar *msg;
|
||||||
gchar *tofree = NULL;
|
gchar *tofree = NULL;
|
||||||
const gchar *ret;
|
const gchar *ret;
|
||||||
gint saved_errno = errno;
|
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))
|
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);
|
ret = g_intern_string (msg);
|
||||||
g_free (tofree);
|
g_free (tofree);
|
||||||
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user