Revert "Revert "Fix unsafe locale usage in wcstod_l fallback""

This reverts commit c15a702f18.

The tests are no longer broken after rerunning CMake.
This commit is contained in:
ridiculousfish 2019-01-19 13:23:19 -08:00
parent 3847d2e9d1
commit b1f5cb9bf4

View file

@ -394,13 +394,13 @@ int flock(int fd, int op) {
// For platforms without wcstod_l C extension, wrap wcstod after changing the // For platforms without wcstod_l C extension, wrap wcstod after changing the
// thread-specific locale. // thread-specific locale.
double fish_compat::wcstod_l(const wchar_t *enptr, wchar_t **endptr, locale_t loc) { double fish_compat::wcstod_l(const wchar_t *enptr, wchar_t **endptr, locale_t loc) {
char *saved_locale = strdup(setlocale(LC_NUMERIC, NULL)); // Create and use a new, thread-specific locale
// Yes, this is hardcoded to use the "C" locale. locale_t locale = newlocale(LC_NUMERIC, "C", nullptr);
// That's the only thing we need, and uselocale(loc) broke in my testing. locale_t prev_locale = uselocale(locale);
setlocale(LC_NUMERIC, "C");
double ret = wcstod(enptr, endptr); double ret = wcstod(enptr, endptr);
setlocale(LC_NUMERIC, saved_locale); // Restore the old locale before freeing the locale we created and are still using
free(saved_locale); uselocale(prev_locale);
freelocale(locale);
return ret; return ret;
} }
#endif // defined(wcstod_l) #endif // defined(wcstod_l)