alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Use thread-safe locale functions
@ 2010-04-13 14:50 Rémi Denis-Courmont
  2010-04-13 15:19 ` Clemens Ladisch
  0 siblings, 1 reply; 4+ messages in thread
From: Rémi Denis-Courmont @ 2010-04-13 14:50 UTC (permalink / raw)
  To: alsa-devel

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(-)

diff --git a/src/conf.c b/src/conf.c
index 570c90f..ce252ab 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -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)
-- 
1.7.0.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Use thread-safe locale functions
  2010-04-13 14:50 [PATCH] Use thread-safe locale functions Rémi Denis-Courmont
@ 2010-04-13 15:19 ` Clemens Ladisch
  2010-04-13 15:25   ` Rémi Denis-Courmont
  2010-04-13 15:54   ` [PATCH] Check if uselocale() is present Rémi Denis-Courmont
  0 siblings, 2 replies; 4+ messages in thread
From: Clemens Ladisch @ 2010-04-13 15:19 UTC (permalink / raw)
  To: Rémi Denis-Courmont; +Cc: alsa-devel

Rémi Denis-Courmont wrote:
> 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
Alsa-devel@alsa-project.org
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Use thread-safe locale functions
  2010-04-13 15:19 ` Clemens Ladisch
@ 2010-04-13 15:25   ` Rémi Denis-Courmont
  2010-04-13 15:54   ` [PATCH] Check if uselocale() is present Rémi Denis-Courmont
  1 sibling, 0 replies; 4+ messages in thread
From: Rémi Denis-Courmont @ 2010-04-13 15:25 UTC (permalink / raw)
  To: Clemens Ladisch; +Cc: alsa-devel

Le mardi 13 avril 2010 18:19:50 Clemens Ladisch, vous avez écrit :
> Rémi Denis-Courmont wrote:
> > 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?

-- 
Rémi Denis-Courmont
http://www.remlab.net/
http://fi.linkedin.com/in/remidenis
_______________________________________________
Alsa-devel mailing list
Alsa-devel@alsa-project.org
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] Check if uselocale() is present
  2010-04-13 15:19 ` Clemens Ladisch
  2010-04-13 15:25   ` Rémi Denis-Courmont
@ 2010-04-13 15:54   ` Rémi Denis-Courmont
  1 sibling, 0 replies; 4+ messages in thread
From: Rémi Denis-Courmont @ 2010-04-13 15:54 UTC (permalink / raw)
  To: alsa-devel

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(-)

diff --git a/configure.in b/configure.in
index 9d985b8..e62e3bd 100644
--- a/configure.in
+++ b/configure.in
@@ -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)
diff --git a/src/conf.c b/src/conf.c
index ce252ab..d1d4a5f 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -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)
-- 
1.7.0.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-04-13 15:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-13 14:50 [PATCH] Use thread-safe locale functions Rémi Denis-Courmont
2010-04-13 15:19 ` Clemens Ladisch
2010-04-13 15:25   ` Rémi Denis-Courmont
2010-04-13 15:54   ` [PATCH] Check if uselocale() is present Rémi Denis-Courmont

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).