Skip to contentSkip to conversation

Use thread-safe locale functions

PATCH In review Rémi Denis-Courmont 2 participants 4 messages Apr 13, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Use thread-safe locale functions

setlocale() is not thread-safe. It can actually trigger a crash if another thread uses locale informations at the same time in the process. Library code should use POSIX newlocale/duplocale/uselocale/freelocale instead. Those functions only change the locale data for the calling thread.

---
 src/conf.c |   16 +++++++---------
 1 files changed, 7 insertions(+), 9 deletions(-)
src/conf.c +7 -9
index 570c90f..ce252ab 100644
@@ -499,22 +499,20 @@ static int safe_strtod(const char *str, double *val)
 {
 	char *end;
 	double v;
-	char *saved_locale;
-	char locstr[64]; /* enough? */
+	locale_t saved_locale, c_locale;
 	int err;
 
 	if (!*str)
 		return -EINVAL;
-	saved_locale = setlocale(LC_NUMERIC, NULL);
-	if (saved_locale) {
-		snprintf(locstr, sizeof(locstr), "%s", saved_locale);
-		setlocale(LC_NUMERIC, "C");
-	}
+	c_locale = newlocale(LC_NUMERIC_MASK, "C", 0);
+	saved_locale = uselocale(c_locale);
 	errno = 0;
 	v = strtod(str, &end);
 	err = -errno;
-	if (saved_locale)
-		setlocale(LC_NUMERIC, locstr);
+	if (c_locale != (locale_t)0) {
+		uselocale(saved_locale);
+		freelocale(c_locale);
+	}
 	if (err)
 		return err;
 	if (*end)
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 4 lines · Rémi Denis-Courmont

setlocale() is not thread-safe. It can actually trigger a crash if another thread uses locale informations at the same time in the process. Library code should use POSIX newlocale/duplocale/uselocale/freelocale instead.

I don't think these functions are available in small embedded C libraries like dietlibc.

Regards,
Clemens
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#2 lore raw

Le mardi 13 avril 2010 18:19:50 Clemens Ladisch, vous avez écrit :

Quoted 8 lines
Quoted 4 lines · Rémi Denis-Courmont

setlocale() is not thread-safe. It can actually trigger a crash if another thread uses locale informations at the same time in the process. Library code should use POSIX newlocale/duplocale/uselocale/freelocale instead.

I don't think these functions are available in small embedded C libraries like dietlibc.

And you're suggesting what? That we continue to let alsa-lib crash programs that use it?

Signature · 7 lines
Rémi Denis-Courmont
http://www.remlab.net/
http://fi.linkedin.com/in/remidenis
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#3 lore raw

PATCH 1/1 Check if uselocale() is present

In reply to Clemens Ladisch

Otherwise, assume the C run-time does not depend on the locale (at least not to format floating point numbers).

---
 configure.in |    1 +
 src/conf.c   |    6 ++++++
 2 files changed, 7 insertions(+), 0 deletions(-)
configure.in +1 -0
index 9d985b8..e62e3bd 100644
@@ -64,6 +64,7 @@ dnl Checks for library functions.
 AC_PROG_GCC_TRADITIONAL
 AC_CHECK_FUNC([hsearch_r], [HAVE_HSEARCH_R=yes])
 AM_CONDITIONAL(ALSA_HSEARCH_R, [test "x$HAVE_HSEARCH_R" != xyes])
+AC_CHECK_FUNCS([uselocale])
 
 SAVE_LIBRARY_VERSION
 AC_SUBST(LIBTOOL_VERSION_INFO)
src/conf.c +6 -0
index ce252ab..d1d4a5f 100644
@@ -499,20 +499,26 @@ static int safe_strtod(const char *str, double *val)
 {
 	char *end;
 	double v;
+#ifdef HAVE_USELOCALE
 	locale_t saved_locale, c_locale;
+#endif
 	int err;
 
 	if (!*str)
 		return -EINVAL;
+#ifdef HAVE_USELOCALE
 	c_locale = newlocale(LC_NUMERIC_MASK, "C", 0);
 	saved_locale = uselocale(c_locale);
+#endif
 	errno = 0;
 	v = strtod(str, &end);
 	err = -errno;
+#ifdef HAVE_USELOCALE
 	if (c_locale != (locale_t)0) {
 		uselocale(saved_locale);
 		freelocale(c_locale);
 	}
+#endif
 	if (err)
 		return err;
 	if (*end)
Signature · 1 line
1.7.0.4