alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
From: "Rémi Denis-Courmont" <remi@remlab.net>
To: alsa-devel@alsa-project.org
Subject: [PATCH] Use thread-safe locale functions
Date: Tue, 13 Apr 2010 17:50:17 +0300	[thread overview]
Message-ID: <1271170217-10904-1-git-send-email-remi@remlab.net> (raw)

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

             reply	other threads:[~2010-04-13 14:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-13 14:50 Rémi Denis-Courmont [this message]
2010-04-13 15:19 ` [PATCH] Use thread-safe locale functions 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1271170217-10904-1-git-send-email-remi@remlab.net \
    --to=remi@remlab.net \
    --cc=alsa-devel@alsa-project.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).