Skip to content

lib: parse and format floats without switching the thread locale - #314

Merged
hyperrealm merged 2 commits into
hyperrealm:masterfrom
affan-arch:restore-thread-locale
Nov 21, 2026
Merged

hyperrealm merged 2 commits into
hyperrealm:masterfrom
affan-arch:restore-thread-locale

Conversation

@affan-arch

@affan-arch affan-arch commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

config_read*() / config_write() switched the calling thread to the "C" numeric locale and the restore didn't put back what the thread had (on Windows it always disabled per-thread locales, on POSIX it always moved the thread to LC_GLOBAL_LOCALE), so a thread with its own locale came back with its number formatting changed under it. This drops the locale switching entirely: floats are parsed with strtod_l() (_strtod_l() on Windows) against a "C" numeric locale object created per read/write call, and formatted with snprintf_l() on macOS, _snprintf_l() on Windows, and elsewhere with snprintf() plus replacing localeconv()->decimal_point with .. There's a new strtod_l check in configure.ac and CMake; without it parsing falls back to plain strtod() as before. The new ThreadLocaleUnchanged test reads and writes a float with a de_DE thread locale and checks the value, the written text, and that the thread's locale is unchanged; it fails on current master.

@hyperrealm

Copy link
Copy Markdown
Owner

I was meaning to look into the locale handling. I think there is a better solution for formatting and parsing floating point values that doesn't require switching the current locale at all.

Most platforms have strtod_l() (_strtod_l() on Windows, which can be aliased to strtod_l() in wincompat.h) for converting string to double, for a given locale.

MacOS and Windows have snprintf_l() / _snprintf_l() for going the other way, double to string. Linux doesn't have that, but we could instead use normal snprintf() and then just post-process the string to change the radix symbol from localeconv()->decimal_point to '.'

config_read*() and config_write() switched the calling thread to the "C"
numeric locale for the duration of the call, and the restore didn't put
back what the thread had: on Windows it always disabled per-thread
locales, so a thread that had set its own numeric locale (say de-DE)
came back on the process-wide one, and on POSIX it always moved the
thread to LC_GLOBAL_LOCALE, dropping any locale it had installed with
uselocale().

Stop touching the thread's locale at all. Floats are parsed with
strtod_l() (_strtod_l() on Windows) against a "C" numeric locale object
created for each read or write, and formatted with snprintf_l() on
macOS and _snprintf_l() on Windows. Where there's no snprintf_l(), they
are formatted with snprintf() and the locale's radix character from
localeconv() is replaced with '.'.

The new ThreadLocaleUnchanged test reads and writes a float while the
thread has its own locale (de_DE where installed) and checks both the
value and the text, and that the thread's locale is unchanged
afterwards.
@affan-arch
affan-arch force-pushed the restore-thread-locale branch from 353c32d to fbc0e82 Compare November 19, 2026 19:04
@affan-arch affan-arch changed the title lib: restore the caller's thread locale after config_read and config_write lib: parse and format floats without switching the thread locale Nov 19, 2026
@affan-arch

Copy link
Copy Markdown
Contributor Author

agreed, that's cleaner. reworked it that way and force-pushed (also rebased on master, it was conflicting after #315).

the thread's locale isn't touched at all now. parsing uses strtod_l() (aliased to _strtod_l() in wincompat.h) against a "C" LC_NUMERIC locale that's created per config_read/config_write call, so there's still no global state. formatting uses snprintf_l() on macOS, _snprintf_l() on Windows, and snprintf() with localeconv()->decimal_point swapped for '.' everywhere else. I added a strtod_l check to configure.ac and CMake, and without it parsing falls back to plain strtod() like before.

the test now reads and writes a float with a de_DE thread locale and checks the value, the written text and that the locale is unchanged. it passes on macOS and glibc (cmake and autotools). I haven't been able to run the msvc branch on windows yet, so that part is worth a second look.

Comment thread lib/util.c Outdated
size_t len = strlen(radix);

*p = '.';
memmove(p + 1, p + len, strlen(p + len) + 1);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (len > 1) { memmove... }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, the memmove is only there for a multibyte radix now. I also got to build it with msvc on windows in the meantime, and the full suite including ThreadLocaleUnchanged passes there.

@hyperrealm

Copy link
Copy Markdown
Owner

looks good. thanks for doing this!

@hyperrealm
hyperrealm merged commit 3fd494b into hyperrealm:master Nov 21, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants