Skip to content

parse decimal float literals independent of the C locale - #2757

Merged
sbc100 merged 4 commits into
WebAssembly:mainfrom
aizu-m:float-literal-locale
Jun 10, 2026
Merged

sbc100 merged 4 commits into
WebAssembly:mainfrom
aizu-m:float-literal-locale

Conversation

@aizu-m

@aizu-m aizu-m commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Noticed while embedding libwabt in a tool that had called setlocale(LC_ALL, "") on a German machine: a valid float literal would not parse.

LC_NUMERIC=de_DE.UTF-8
ParseFloat(LiteralType::Float, "1.5")  ->  Result::Error

FloatParser::Strto hands the token to strtof/strtod, which read the radix character from the current locale's LC_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.cc already hand-rolls C-locale ctype helpers for the same reason.

Normalise . to the locale's decimal point before the strto* 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.5 under a comma-decimal locale, and skips when none is installed.

@sbc100

sbc100 commented Jun 9, 2026

Copy link
Copy Markdown
Member

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.
@aizu-m
aizu-m force-pushed the float-literal-locale branch from 0afd7c6 to 1651cff Compare June 10, 2026 04:33
@aizu-m

aizu-m commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

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.

@sbc100

sbc100 commented Jun 10, 2026

Copy link
Copy Markdown
Member

What about just doing setlocale(LC_NUMERIC, "C"); on startup? I think that might be enough to make the existing code work.

@aizu-m

aizu-m commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/literal.cc Resolved
Comment thread src/literal.cc Resolved

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm, with couple of final cleanups

Comment thread src/literal.cc Outdated
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about a shorter comment like:

Always use the C locale for parsing floats since the Wat grammar requires `.` for the radix.

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, swapped in your wording.

Comment thread src/literal.cc Outdated
#if defined(_WIN32)
#define strtof_l _strtof_l
#define strtod_l _strtod_l
using CLocale = _locale_t;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

#define locale_t _locale_t

This avoid the new CLocale type.

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.

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.

Comment thread src/literal.cc Outdated

// 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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

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, 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.

@sbc100

sbc100 commented Jun 10, 2026

Copy link
Copy Markdown
Member

Looks like you just need to fix the lint errors (git clang-format origin/main) now.

Thanks!

@aizu-m

aizu-m commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

Done. git clang-format wrapped the long _GNU_SOURCE comment and moved the locale.h include up; suite still 131/131 locally.

@sbc100
sbc100 merged commit b7ad849 into WebAssembly:main Jun 10, 2026
17 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