From 5745f932e8189da386c22a1ec962c1a60ca3a6f8 Mon Sep 17 00:00:00 2001 From: Nicolas Piguet Date: Tue, 10 Mar 2015 11:02:28 +0100 Subject: [PATCH] change sass_atof to be thread safe --- util.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/util.cpp b/util.cpp index 50bc915a9d..eedf11f310 100644 --- a/util.cpp +++ b/util.cpp @@ -24,11 +24,24 @@ namespace Sass { /* Locale unspecific atof function. */ double sass_atof(const char *str) { - char* locale = setlocale(LC_NUMERIC, NULL); - setlocale(LC_NUMERIC, "C"); - double val = atof(str); - setlocale(LC_NUMERIC, locale); - return val; + char separator = *(localeconv()->decimal_point); + if(separator != '.'){ + // The current locale specifies another + // separator. convert the separator to the + // one understood by the locale if needed + const char *found = strchr(str, '.'); + if(found != NULL){ + // substitution is required. perform the substitution on a copy + // of the string. This is slower but it is thread safe. + char *copy = sass_strdup(str); + *(copy + (found - str)) = separator; + double res = atof(copy); + free(copy); + return res; + } + } + + return atof(str); } // double escape every escape sequences