Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XPEm6-0003Cv-Mb for pgsql-odbc@arkaria.postgresql.org; Wed, 03 Sep 2014 17:55:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XPEm5-0000k1-Hb for pgsql-odbc@arkaria.postgresql.org; Wed, 03 Sep 2014 17:55:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XPEm4-0000jL-GG for pgsql-odbc@postgresql.org; Wed, 03 Sep 2014 17:55:16 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XPElw-0007QN-IF for pgsql-odbc@postgresql.org; Wed, 03 Sep 2014 17:55:14 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 5C52128B87 for ; Wed, 3 Sep 2014 10:55:06 -0700 (PDT) Received: from EX13-CAS-012.vmware.com (EX13-CAS-012.vmware.com [10.113.191.64]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 531D8EC2C2 for ; Wed, 3 Sep 2014 10:55:06 -0700 (PDT) Received: from EX13-MBX-024.vmware.com (10.113.191.44) by EX13-MBX-003.vmware.com (10.113.191.23) with Microsoft SMTP Server (TLS) id 15.0.775.38; Wed, 3 Sep 2014 10:55:00 -0700 Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.775.38; Wed, 3 Sep 2014 10:54:44 -0700 Message-ID: <540755E1.50700@vmware.com> Date: Wed, 3 Sep 2014 20:54:41 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.0 MIME-Version: 1.0 To: "pgsql-odbc@postgresql.org" Subject: localeconv() and setlocale() thread-safety Content-Type: multipart/mixed; boundary="------------090401000006080800070402" X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -8.6 (--------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org --------------090401000006080800070402 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit 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 --------------090401000006080800070402 Content-Type: text/x-diff; name="fix-locale-race-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-locale-race-1.patch" diff --git a/convert.c b/convert.c index 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; } } --------------090401000006080800070402 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --------------090401000006080800070402--