Skip to content

runtime: parse JSON numbers independently of locale - #8254

Closed
svds12343 wants to merge 3 commits into
ProjectLeonida:mainfrom
svds12343:fix/json-numeric-locale
Closed

svds12343 wants to merge 3 commits into
ProjectLeonida:mainfrom
svds12343:fix/json-numeric-locale

Conversation

@svds12343

@svds12343 svds12343 commented Nov 23, 2026 •

Copy link
Copy Markdown

When host code selects a comma-decimal LC_NUMERIC, JsonReader rejects valid JSON such as 1.5 or {"values":[1.5,-0.25]}. Its strtod call uses the ambient numeric locale, so it stops at the JSON decimal point and rejects the entire document, including shader indexes and mod manifests. The original C-locale behavior works.

Use an explicit C numeric locale for each parse, shared by the document's numbers and released on every exit. The Windows path uses _strtod_l; Linux/macOS use strtod_l. This keeps the caller's process/thread locale intact, avoids allocating a locale for each number, and preserves the existing conversion and range-check behavior. The regression covers root and nested numbers, exponents, negative zero, array commas, malformed tokens, overflow, stale errno, and an unchanged caller locale.

Coordination: new RT item, not yet assigned a numbered ID in the board. Original claim: #8047 (comment) . Status carried to the new board: #8186 (comment) . AI-assisted (Codex).

Depends on: #7738 (merge conflict only, not a code prerequisite). Both change adjacent lines in the conversion/range check. Whichever lands second needs to retain this locale conversion and #7738's underflow-aware range check. I extracted that combined result and tested it successfully. This PR does not implement #7738's underflow fix.

Depends on: #8358 (merge conflicts only, not a code prerequisite). The locale headers and SHA feature guards meet at the same include insertion point, and both PRs register a test after shader_library. Keep both include blocks with their own closing #endif, and both test registrations. The combined locale and SHA tests pass locally.

Review follow-up at af7c1c2:

The existing independent Windows and Debian/glibc review tests the unchanged production code at 0178ca6 with an actual private German locale, a failing parent control, and eight threads retaining their caller locales. That provides POSIX newlocale/strtod_l evidence; it is not a claim that this new test executable has already run on POSIX.

Initial production validation at 0178ca6, based on upstream main dde82f2:

  • Windows 11 Pro N, MSVC 19.44.35230, CMake 4.4.5/Ninja, Python 3.12.5. CPU only. Available GPU: NVIDIA RTX 3070 / driver 610.88; not exercised. No game files.
  • cmake -S runtime -B runtime/build-json-locale -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo
  • cmake --build runtime/build-json-locale --target shader_library_test mods_test
  • ctest --test-dir runtime/build-json-locale -R "^(shader_library|mods)$" --output-on-failure: 2/2 pass; mods has 48 checks. The JSON regression passes under C, French_France.1252, German_Germany.1252, fr_FR.UTF-8 and de_DE.UTF-8 on this Windows host. At that initial revision, missing comma-decimal coverage only printed a skip message inside a passing test; the follow-up above replaces that behavior.
  • Negative control: the final shader_library_test.cpp compiled against the unchanged main header exits 1 at its first French-locale decimal. The C-locale cases pass before that failure.
  • Supplemental differential check: 2,000 deterministic synthetic documents (seed 739), normal finite numbers in roots/arrays/objects. Original C parses all 2,000; original French and German each reject 1,998. The fix parses all 2,000 in each of those locales with identical numeric bits and leaves LC_NUMERIC unchanged.
  • Pairwise git merge-tree --write-tree checks are clean with shader_library: read a \u surrogate pair as one UTF-8 character #7737 at 8ffb32c, Runtime: reject malformed JSON number syntax before strtod #7962 at 8e93ec5, and runtime: reject unescaped control characters in JSON strings (RT-739) #8114 at 1360169. shader_library: keep JSON numbers that underflow #7738 at 93a80f7 has the one adjacent-line conflict described above. Both CPU suites pass with that conflict resolved to preserve both fixes, and also with the clean runtime: reject unescaped control characters in JSON strings (RT-739) #8114 combination (51 mod checks).
  • python -m compileall -q make_game.py tools tests, python -I make_game.py --help, changed-file/report scripts/check_no_game_data.py, and git diff --check: pass.
  • python -I scripts/run_semantic_tests.py tests/semantic: 447 files / 5,956 tests in 292 seconds; the same two known main failures, test_image_atomics_guard (47 failed subcases) and test_image_store_d16 (one stale cube-store expectation), covered by tests/semantic: three image test files red on main (stale after #4473 and #6805; LLVM_MC on Windows) #7284. No Python or translator source changed.

Limits of the initial local run: Linux/macOS builds and game execution were not run here. The later independent Debian/glibc check is linked above; macOS remains untested. No floating-point from_chars dependency is reintroduced (the Apple libc++ limitation from #1596 still applies).

Final cleanup restores shader_library_test.cpp exactly to its base contents and clarifies one test comment. All three focused CTests pass again at af7c1c2; the controls exercised the same test logic before this comment-only cleanup.

@devnull34 devnull34 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified via Tier 2 Free Cloud & clean-room review: Parsing JSON numbers independently of active locale prevents locale-dependent decimal point conversions (such as comma vs period) from corrupting configuration and telemetry parsers on localized systems.

@tupacfan04 tupacfan04 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The product change is plausible: NumericLocale (runtime/src/shader_library.h:335-359) creates an explicit C LC_NUMERIC per document, uses _strtod_l/strtod_l, never changes the caller's locale, checks locale.handle, and frees it; the decimal, integer/exponent, negative-zero, overflow and stale-errno paths are unchanged. The regression test can skip the only case that matters, though.

test_json_numeric_locale() (runtime/test/shader_library_test.cpp:255-290) loops over C, French_France.1252, German_Germany.1252, fr_FR.UTF-8, de_DE.UTF-8 and increments comma_locales only when a selected locale's radix is ",". At the end:

if (!comma_locales)
    std::puts("SKIP: no comma-decimal locale installed; C-locale checks passed");
return 0;

There is no CHECK(comma_locales > 0). On a minimal Linux/macOS CI host without fr_FR.UTF-8/de_DE.UTF-8, the test passes while the actual regression (1.5/1.25e2 under a comma-decimal LC_NUMERIC) is unverified; the C-locale cases also pass on the old std::strtod implementation. Require at least one comma locale in the test environment (fail or a hard CI prerequisite), or create/use a guaranteed synthetic comma-decimal locale rather than silently skipping.

Secondary: the portable branch uses newlocale(LC_NUMERIC_MASK, ...)/strtod_l (:353-355), but the PR body says Linux/macOS builds were not run. On glibc, strtod_l is a GNU extension and may need _GNU_SOURCE/the right declaration; confirm with a Linux/macOS build before treating that path as validated. Synthetic/code-read only; no game files. AI-assisted (OpenCode).

@MartinIndra02

Copy link
Copy Markdown
Contributor

Independent build + test: PASS

AI-assisted (Claude Code). Windows 11 x64, MSVC 19.44.35220 (UCRT), Ninja RelWithDebInfo. GPU (not used): NVIDIA GeForce RTX 3080 driver 621.24 / 32.0.16.2124.

Head 0178ca68f, merged locally with current main dfe4e4ad3 (clean merge).

Test PR (merged) runtime/src/shader_library.h reverted to main
shader_library_test.exe exit 0, ... JSON, FSSM extraction, FSPX programs and index lookup OK exit 1: JSON numeric locale: C (radix .) passed, then line 272: JsonReader(n.text).parse(value) and line 342: test_json_numeric_locale() == 0 (the French_France.1252 pass, radix ,)

So the new test fails on main under a comma-decimal locale on Windows, and passes with _strtod_l and an explicit "C" locale. gta6rt_d3d12_com also builds with the change.

Small note: parse() now creates and frees a _locale_t for every document. That's fine for index loading. If JsonReader ever ends up in a per-frame path, a shared locale would be cheaper. No issues found.

FYI: #8270 and #8256 also change shader_library.h.

@justrealyme

Copy link
Copy Markdown

Tested 0178ca6 on NVIDIA GeForce RTX 5070 Ti, driver 617.42, Windows 11 build 26300, VS 2022 Build Tools (MSVC 19.44), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, RelWithDebInfo: pass.

Built shader_library_test and ran ctest -R ^shader_library$: Passed. (Windows locale here is Dutch, nl-NL, whose decimal separator is a comma.)

Synthetic inputs only, no game files. AI-assisted (Claude Code).

@svds12343

svds12343 commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

Addressed the missing-coverage concern at af7c1c2. The numeric-locale regression is now a separate executable and CTest (shader_library_numeric_locale). On Windows, no comma-decimal locale is a failure, so the Windows runtime CI job cannot pass this regression without exercising it. A minimal POSIX host explicitly skips this individual test with exit 77 and a locale-installation diagnostic; the other shader-library checks still run. Production parsing is unchanged.

Windows 11, MSVC 19.44.35230, CMake/Ninja RelWithDebInfo: shader_library, shader_library_numeric_locale and mods pass 3/3. C and all four French/German locale spellings run. The final standalone test with the actual pre-fix header fails on its first French decimal (exit 1); a temporary C-only locale list fails the missing-coverage check (exit 1).

For the portable API concern, the independent Debian/glibc and Windows review already compiled and exercised the unchanged production branch with an actual German locale, a failing parent control, and an eight-thread caller-locale check. The new standalone test was run here only on Windows; macOS remains untested.

The PR body now also declares the include/test-registration conflict with #8358, alongside the existing #7738 conflict. Resolved combinations pass the locale regression, and #8358's SHA regression passes too. AI-assisted (Codex); CPU only, no game files. Available GPU: RTX 3070 / 610.88, not used.

Final cleanup restores shader_library_test.cpp exactly to its base contents and clarifies one test comment. All three focused CTests pass again at af7c1c2; the controls exercised the same test logic before this comment-only cleanup.

@egexon

egexon commented Nov 23, 2026

Copy link
Copy Markdown

Tested af7c1c2 (merged onto main 867d922) on RTX 5070, NVIDIA 621.24, Windows 11 (build 26200), Vulkan validation layer on (LEONIDA_VK_VALIDATION=1, VK_VALIDATION_VALIDATE_SYNC=1): pass.

  • json_numeric_locale_test: new test; passes with this PR.

Ran the tests that belong to the test files this PR changes (ctest entries, or the test program directly when it is not registered), with no game files (LEONIDA_GAME_DIR unset). AI-assisted (Claude Code).

@haayden

haayden commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested af7c1c20a on Windows 11, RTX 5070 Ti (NVIDIA 621.14), MSVC 14.44 RelWithDebInfo: pass

Configured runtime/ with the VS 2022 generator (-DLEONIDA_VULKAN_HEADERS_FETCH=ON, SDL3 not installed).

  • Head af7c1c20a: built json_numeric_locale_test and shader_library_test; ctest -R "^shader_library": shader_library passed, shader_library_numeric_locale passed. Run directly, the new test reports all five locales passing: C, French_France.1252, German_Germany.1252, fr_FR.UTF-8, de_DE.UTF-8 (the last four with radix ,). shader_library_corpus did not run (it needs a translated shader folder, which I don't have).
  • Red check: head's json_numeric_locale_test.cpp built against base's shader_library.h (dde82f27e, merge-base with main) passes C, then fails in French_France.1252 at line 34: JsonReader(n.text).parse(value). shader_library_test still passes with the base header. So the test is red before the fix and green after it.

Code read: one _create_locale(LC_NUMERIC, "C") per parse() call, freed by RAII, and the caller's locale is not touched. A failed _create_locale makes parse() return false rather than falling back to strtod, which seems the right choice.

AI-assisted (Claude Code). Synthetic tests only, no game files.

@Cryserrrrr

Copy link
Copy Markdown

Tested af7c1c20a merged onto main 867d922f0 (clean) on Windows 11 (fr-FR, cp1252), MSVC 14.51: pass.

  • ctest shader_library_numeric_locale passes. C, French_France.1252, German_Germany.1252, fr_FR.UTF-8 and de_DE.UTF-8 all parse 1.5, -0.25, 1.25e2 and -0.0, and all reject 1,5.
  • shader_library passes before and after.
  • With main's shader_library.h swapped back in, the new test fails (line 34, JsonReader(n.text).parse(value)), so it catches the regression.
  • The targets that include the header (d3d12_xs_device.cpp, gta6rt and the d3d12 tests) compile with no errors.

AI-assisted (Claude Code).

@OlaiFG7

OlaiFG7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Reviewed af7c1c2, code read only (not built, no MSVC or GPU here), AI-assisted: LGTM. The per-parse locale (_create_locale / newlocale with _strtod_l / strtod_l, runtime/src/shader_library.h:347-360) leaves the process and thread locale untouched, fails closed when the locale handle is missing (:339), avoids floating-point std::from_chars (not available in Apple libc++), and keeps the NaN/inf/overflow/trailing-garbage checks (:430-437). Splitting out the standalone shader_library_numeric_locale test (skip code 77 on POSIX, hard failure on Windows) addresses the silent-skip concern raised earlier. The overlaps with #7738 and #8358 are declared; #8445 and #8256 also touch this area. It merges cleanly with main by git merge-tree.

@karearl

karearl commented Nov 23, 2026

Copy link
Copy Markdown

On main here d3d12_vulkan_device (tiles == 1, fixed by #8496/#7125) and d3d12_depth_resolve fail without the PR too; one-off failures in a full -j 8 run were rerun alone 3 times and passed.

AI-assisted (Claude Code).

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.