Repository navigation
parse decimal float literals independent of the C locale - #2757
Conversation
|
Can we not find a way to configure the existing code to ignore the locale? |
FloatParser::Strto handed decimal literals to strtof/strtod, which read the radix character from the current locale's LC_NUMERIC category. The wat grammar always writes the radix as '.', so when the embedder has set a locale whose decimal point is ',' (for example de_DE) strtod stops at the '.' and a valid literal such as 1.5 is rejected. Parse against a fixed "C" locale instead of the global one: strtof_l/strtod_l on POSIX and _strtof_l/_strtod_l on MSVC, with a process-wide "C" locale_t created on first use. The hex and integer paths were never affected. Adds a regression test that parses 1.5 under a comma-decimal locale and skips when none is installed.
0afd7c6 to
1651cff
Compare
|
Makes sense, that's cleaner. Reworked it that way: Strto now parses against a fixed "C" locale instead of the global one, via strtof_l/strtod_l (and _strtof_l/_strtod_l on MSVC) with a locale_t created once on first use. The embedder's LC_NUMERIC never feeds the parse anymore, so the '.' radix is honoured whatever they've set. The radix-rewriting helper is gone. Verified the same way I hit the bug: under de_DE the "1.5" regression test now passes, and the full unittest suite stays at 131/131. The hex and integer paths were never affected. |
|
What about just doing |
|
For wabt's own binaries that would be enough, but they are already unaffected: a process starts in the "C" locale and none of the tools call setlocale, so the existing code only breaks when something else changes the locale. That something is the embedder. Where I hit this, libwabt was linked into a host app that had called setlocale(LC_ALL, "") for its own UI formatting. There is no wabt startup to hook in that case, and the library calling setlocale(LC_NUMERIC, "C") itself would flip the host's printf/scanf formatting behind its back; it is process-global and not thread-safe either. The _l variants confine the "C" parse to literal.cc and leave the embedder's locale alone, which is why I went that way. |
sbc100
left a comment
There was a problem hiding this comment.
lgtm, with couple of final cleanups
| // category, but the wat grammar always writes the radix as '.'. Under a locale | ||
| // whose decimal point is not '.' (for example ',' in the German locale) strtod | ||
| // stops at the '.', so a valid literal like "1.5" fails to parse. The embedder | ||
| // is free to set the locale, so parse against a fixed "C" locale instead. |
There was a problem hiding this comment.
How about a shorter comment like:
Always use the C locale for parsing floats since the Wat grammar requires `.` for the radix.
There was a problem hiding this comment.
Done, swapped in your wording.
| #if defined(_WIN32) | ||
| #define strtof_l _strtof_l | ||
| #define strtod_l _strtod_l | ||
| using CLocale = _locale_t; |
There was a problem hiding this comment.
#define locale_t _locale_t
This avoid the new CLocale type.
There was a problem hiding this comment.
The CLocale alias is gone. With your initialiser snippet below the two branches just use _locale_t/locale_t directly, so the extra define wasn't needed either.
|
|
||
| // The "C" locale never changes, so one handle is shared by every parse and | ||
| // intentionally lives for the rest of the process. | ||
| static CLocale c_locale = MakeCLocale(); |
There was a problem hiding this comment.
How about just:
#if defined(_WIN32)
static _locale_t c_locale = _create_locale(LC_ALL, "C");
#else
static locale_t c_locale = newlocale(LC_ALL_MASK, "C", nullptr);
#endif
This avoids needing a MakeCLocale function
There was a problem hiding this comment.
Done, took your snippet as-is. MakeCLocale and the alias are both gone; the block is now four lines plus the comment. Rebuilt and the suite is still 131/131.
|
Looks like you just need to fix the lint errors ( Thanks! |
|
Done. git clang-format wrapped the long _GNU_SOURCE comment and moved the locale.h include up; suite still 131/131 locally. |
Noticed while embedding libwabt in a tool that had called setlocale(LC_ALL, "") on a German machine: a valid float literal would not parse.
FloatParser::Strtohands the token tostrtof/strtod, which read the radix character from the current locale'sLC_NUMERIC. The wat grammar always writes the radix as., so under a locale whose decimal point is,strtod stops at the.and the literal is rejected. The caller is free to change the locale;c-writer.ccalready hand-rolls C-locale ctype helpers for the same reason.Normalise
.to the locale's decimal point before thestrto*call. The.case keeps the original path, so overflow and underflow behaviour is unchanged, and hex floats (ParseHex) were never affected.The test parses
1.5under a comma-decimal locale, and skips when none is installed.