Repository navigation
[wasm2c] Write float constants independent of the host locale - #2768
Conversation
| } else { | ||
| Writef("%.9g", Bitcast<float>(f32_bits)); | ||
| char buf[128]; | ||
| snprintf(buf, sizeof(buf), "%.9g", Bitcast<float>(f32_bits)); |
There was a problem hiding this comment.
Can we instead use sprintf_l like we already use the _l flavors of other libc functions?
There was a problem hiding this comment.
Switched to the c_locale approach from literal.cc. One snag: glibc and musl only ship the strto*_l family, not the printf side, so there's no snprintf_l/sprintf_l there. On POSIX I format through the same C locale_t via uselocale; Windows uses _snprintf_l. That drops the radix-rewriting helper and keeps the C-locale output byte-identical.
5a5bb75 to
ccba3a8
Compare
|
No objection to this, but wondering if users might prefer to do #2389 . The benefit is we really don't have to think about floating-point text representations at all, but the downside is it makes the generated C code a little less comprehensible (which is why we never merged it the first time). @aizu-m what do you think? |
| #else | ||
| locale_t prev = uselocale(c_locale); | ||
| int result = snprintf(buf, size, format, value); | ||
| uselocale(prev); |
There was a problem hiding this comment.
I think sadly this is not thread safe, right?
So if we can't have snprintf_l then maybe we have to do with the original approach.
There was a problem hiding this comment.
Makes sense. uselocale is per-thread so it wouldn't race other threads, but it still swaps thread state, and glibc/musl don't ship the _l printf side anyway. Reverted to the NormalizeFloatRadix version: format with snprintf, then rewrite localeconv()->decimal_point back to '.'. No locale state touched, and the C-locale output stays byte-identical. Test still passes under de_DE.
There was a problem hiding this comment.
Oh, TIL that uselocale is per-thread.
ccba3a8 to
385c41a
Compare
|
@keithw my vote is to land this as the targeted fix. It's a few lines and the emitted constants stay readable. #2389 is the more complete answer since it drops decimal text entirely, but it bloats the output and reads less clearly, which is why it stalled last time. The two don't really conflict: this stops the comma-locale miscompile now, and #2389 can still land later on its own merits if the readability tradeoff is acceptable. Happy to follow your call though. |
| @@ -0,0 +1,103 @@ | |||
| /* | |||
| * Copyright 2024 WebAssembly Community Group participants | |||
There was a problem hiding this comment.
Done, bumped to 2026.
|
|
||
| #include <cctype> | ||
| #include <cinttypes> | ||
| #include <clocale> |
There was a problem hiding this comment.
Still needed, I'm afraid. NormalizeFloatRadix calls localeconv(), which is declared in , so I left the include in.
snprintf renders the radix character according to LC_NUMERIC, so under a comma-decimal locale CWriter::Write emitted constants like 100,125: invalid C, or silently a comma expression with a different value. Normalise the radix produced by %g back to '.' so the generated C does not depend on the locale the embedder set.
385c41a to
a03af2f
Compare
|
Landing this for now. We can always land #2389 later if we decide that is the way to go. |
Noticed while running wasm2c with the embedder's locale set to de_DE, where LC_NUMERIC makes the radix ','. The constant writer produced:
snprintf("%.9g"/"%.17g") in CWriter::Write(const Const&) takes the radix from the active locale, but the emitted C must use '.'. The comma is either a compile error or, in argument/initialiser position, a comma expression that quietly drops the fractional part. The f64 path is worse: the following strchr(buf, '.') no longer finds the radix, so it also appends ".0" and writes 1,5.0.
The .wat writer sidesteps this by emitting hex floats, and the parsing direction was already made locale-independent in literal.cc. This is the output side. The fix formats as before, then rewrites the locale's radix back to '.', so the generated C is byte-identical regardless of the locale the caller set. Output under the C locale is unchanged.
Added a unit test that writes a module with f32/f64 constants under a comma locale and checks the result uses '.'. It skips when no comma locale is installed, matching the existing parser test.