From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: pgsql-odbc@postgresql.org <pgsql-odbc@postgresql.org>
Subject: localeconv() and setlocale() thread-safety
Date: Wed, 3 Sep 2014 20:54:41 +0300
Message-ID: <540755E1.50700@vmware.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>
convert.c has a mechanism to convert the decimal separator in numeric
strings between '.' and the separator of the current locale (e.g. ',' in
Finnish).
The implementation is not thread-safe. If you switch the locale in an
application, and then run a query in two threads concurrently, and they
both call current_numeric_locale() at the same time, they might both try
to free the old current_locale string, causing a crash.
That might seem unlikely to happen in practice, but I just received a
stack trace from a crash dump from a testing environment that contained
words "psqlodbc", "lconv", and "free". The stack trace was missing/had
garbled symbols, so I couldn't make more sense of it, but
current_numeric_locale() is the only place in psqlodbc that deals with
lconv structs. The crash may well have been something completely
unrelated, but this seems worth fixing anyway.
There are more issues that we can't do much about, if an application
changes the locale in one thread, while another thread is running a
query. In that situation, the decimal separator we got from localeconv()
might not match the decimal separator that our earlier call to sprintf()
used. But as I said, there isn't much we can do about that, since there
is no thread-safe version of localeconv().
I'll commit the attached patch that fixes the first problem, unless
someone has a better idea.
PS. Does anyone know an example of a locale that uses a multi-byte
character as the decimal separator?
- Heikki
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbcAttachments:fix-locale-race-1.patch (text/x-diff, 2.9K) downloaddiff --git a/convert.c b/convert.cindex d440071..dafba6e 100644--- a/convert.c+++ b/convert.c@@ -612,34 +612,49 @@ interval2istruct(SQLSMALLINT ctype, int precision, const char *str, SQL_INTERVAL
#ifdef HAVE_LOCALE_H
-static char *current_locale = NULL;-static char *current_decimal_point = NULL;-static void current_numeric_locale(void)+/*+ * Get the decimal point of the current locale.+ *+ * XXX: This isn't thread-safe, if another thread changes the locale with+ * setlocale() concurrently. There are two problems with that:+ *+ * 1. The pointer returned by localeconv(), or the lc->decimal_point string,+ * might be invalidated by calls in other threads. Until someone comes up+ * with a thread-safe version of localeconv(), there isn't much we can do+ * about that. (libc implementations that return a static buffer (like glibc)+ * happen to be safe from the lconv struct being invalidated, but the+ * decimal_point string might still not point to a static buffer).+ *+ * 2. The between the call to sprintf() and get_current_decimal_point(), the+ * decimal point might change. That would cause set_server_decimal_point()+ * to fail to recognize a decimal separator, and we might send a numeric+ * string to the server that the server won't recognize. This would cause+ * the query to fail in the server.+ *+ * XXX: we only take into account the first byte of the decimal separator.+ */+static char get_current_decimal_point(void)
{
- char *loc = setlocale(LC_NUMERIC, NULL);- if (NULL == current_locale || 0 != stricmp(loc, current_locale))- {- struct lconv *lc = localeconv();+ struct lconv *lc = localeconv();- if (NULL != current_locale)- free(current_locale);- current_locale = strdup(loc);- if (NULL != current_decimal_point)- free(current_decimal_point);- current_decimal_point = strdup(lc->decimal_point);- }+ return lc->decimal_point[0];
}
+/*+ * Modify the string representation of a numeric/float value, converting the+ * decimal point from '.' to the correct decimal separator of the current+ * locale.+ */
static void set_server_decimal_point(char *num)
{
+ char current_decimal_point = get_current_decimal_point();
char *str;
- current_numeric_locale();- if ('.' == *current_decimal_point)+ if ('.' == current_decimal_point)
return;
for (str = num; '\0' != *str; str++)
{
- if (*str == *current_decimal_point)+ if (*str == current_decimal_point)
{
*str = '.';
break;
@@ -647,18 +662,21 @@ static void set_server_decimal_point(char *num)
}
}
+/*+ * Inverse of set_server_decimal_point.+ */
static void set_client_decimal_point(char *num)
{
+ char current_decimal_point = get_current_decimal_point();
char *str;
- current_numeric_locale();- if ('.' == *current_decimal_point)+ if ('.' == current_decimal_point)
return;
for (str = num; '\0' != *str; str++)
{
if (*str == '.')
{
- *str = *current_decimal_point;+ *str = current_decimal_point;
break;
}
}
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-odbc@postgresql.org
Cc: hlinnakangas@vmware.com
Subject: Re: localeconv() and setlocale() thread-safety
In-Reply-To: <540755E1.50700@vmware.com>
References: <540755E1.50700@vmware.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox