From b1f5cb9bf490f40b5444e8e2e0498969a3fcd64d Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 19 Jan 2019 13:23:19 -0800 Subject: [PATCH] Revert "Revert "Fix unsafe locale usage in `wcstod_l` fallback"" This reverts commit c15a702f18d5cee8a4e54c486f90467e8729a62e. The tests are no longer broken after rerunning CMake. --- src/fallback.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/fallback.cpp b/src/fallback.cpp index a0db093a9..bcfc4d926 100644 --- a/src/fallback.cpp +++ b/src/fallback.cpp @@ -394,13 +394,13 @@ int flock(int fd, int op) { // For platforms without wcstod_l C extension, wrap wcstod after changing the // thread-specific locale. double fish_compat::wcstod_l(const wchar_t *enptr, wchar_t **endptr, locale_t loc) { - char *saved_locale = strdup(setlocale(LC_NUMERIC, NULL)); - // Yes, this is hardcoded to use the "C" locale. - // That's the only thing we need, and uselocale(loc) broke in my testing. - setlocale(LC_NUMERIC, "C"); + // Create and use a new, thread-specific locale + locale_t locale = newlocale(LC_NUMERIC, "C", nullptr); + locale_t prev_locale = uselocale(locale); double ret = wcstod(enptr, endptr); - setlocale(LC_NUMERIC, saved_locale); - free(saved_locale); + // Restore the old locale before freeing the locale we created and are still using + uselocale(prev_locale); + freelocale(locale); return ret; } #endif // defined(wcstod_l)