Skip to content

Fix advanced-mode JSON float parsing under comma-decimal locales (#15746) - #15865

Merged
Nir-Az merged 2 commits into
realsenseai:developmentfrom
SIDDARTHAREDDY8:oss-bhai-2026-10-22-librealsense-15746
Oct 28, 2026
Merged

Nir-Az merged 2 commits into
realsenseai:developmentfrom
SIDDARTHAREDDY8:oss-bhai-2026-10-22-librealsense-15746

Conversation

@SIDDARTHAREDDY8

@SIDDARTHAREDDY8 SIDDARTHAREDDY8 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15746

Bug

Loading a D400 advanced-mode JSON on a system with a comma-decimal OS locale (German, Danish, ...) truncates every fractional parameter: "0.482143" comes back as "0" and "1.8125" as "1" after a load/save round-trip. Regression reported against 2.56.4 and reproduced by a second user on a Danish locale.

Root cause

In src/ds/advanced_mode/json_loader.hpp, json_struct_field::load() and json_invert_struct_field::load() converted the JSON string values with ::atof(). atof follows the process C locale, so under a comma-decimal locale it stops parsing at the '.' and returns only the integer part.

Fix

  • New parse_json_float() helper parses with an istringstream imbued with std::locale::classic(), so '.' is always the decimal separator. Both ::atof() call sites in the JSON load path now use it.
  • json_struct_field::save() and the JSON-number conversion in update_structs() now also imbue the classic locale, so a load/save round-trip always writes '.' decimals even if the hosting application changed the global locale.

Testing

  • Standalone harness compiling the real json_loader.hpp: before the fix, under a de_DE.UTF-8 C locale, load("0.482143") gave removeThresh 0 (expected 81) and load("1.8125") gave diffThresh 1.0, and save() wrote "0"/"1", exactly the corruption in the issue. After the fix, both the C locale and de_DE.UTF-8 give 81 / 1.8125 and save() writes "0.482143" / "1.8125".
  • Live pytest case test_json_round_trip_keeps_fractions in unit-tests/live/options/pytest-advanced-mode.py (replaces the earlier C++ test, which did not compile in the unit-test build): round-trips fractional advanced-mode values through load_json() under a comma-decimal locale. Verified on D455 / Windows 11 with Regional format de-DE: fails without the fix (values truncated to 0), the whole file passes 19/19 with the fix.
  • The full library cmake build was not run locally; verification was done by compiling and running the actual header under test with g++ 13 / C++17 as described above.

This PR was prepared with AI assistance; every line was reviewed and the fix was verified locally as described above.

…lsenseai#15746)

Signed-off-by: SIDDARTHA REDDY <75976672+SIDDARTHAREDDY8@users.noreply.github.com>

@sysrsbuild-gh-agentic sysrsbuild-gh-agentic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Automated review by rs-agentic bot. Two bugs flagged below; please address before merge.

Comment thread src/ds/advanced_mode/json_loader.hpp Resolved
Comment thread src/ds/advanced_mode/json_loader.hpp Resolved
@Nir-Az

Nir-Az commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix and the test. Some extra context on the root cause:

The regression comes from commit f28d25c ("fix error 42 on unicode user names"). It is in v2.56.4 but not in v2.56.3, and it adds this line to third-party/rsutils/src/special-folder.cpp:

setlocale(LC_ALL, ".UTF8");

On Windows, ".UTF8" selects the user's default locale with the UTF-8 code page. Under a German or Danish region, this makes the decimal separator a comma for the whole process. get_special_folder() runs whenever a context is created (src/context.cpp, plus the rum collector/config), so every librealsense process, Python included, has its locale changed. That is why the bug follows the Windows region rather than the display language. json_loader.hpp itself did not change between v2.56.3 and v2.56.4.

This PR's classic-locale parsing is a good defensive change. However, the global locale switch is still there, and other locale-sensitive parsing remains exposed:

  • std::stof/std::stod in src/media/ros2/ros2_reader.cpp and src/media/ros/ros_reader.cpp (recording playback)
  • std::stof in src/ds/ds-options.cpp
  • std::stof in common/option-model.cpp (the Viewer's float option input)
  • localeconv() in third-party/rsutils/src/json.cpp
  • any printf/atof in the host application

The proper fix is to remove the global setlocale(LC_ALL, ...) from get_special_folder, either by getting the paths through the wide-character Windows APIs and converting them with WideCharToMultiByte(CP_UTF8, ...), or at minimum by limiting it to LC_CTYPE and restoring the previous locale. The parsing changes here can stay as a second line of defence. We will need this validated on both Windows and Linux under a comma-decimal locale.

@sysrsbuild-gh-agentic sysrsbuild-gh-agentic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the new commit. The two previously flagged bugs in json_loader.hpp (imbue-after-construction + unchecked parse at line 37; missing setprecision at line 171) are still unaddressed. One new bug noted below in the replacement Python test.

@Nir-Az Nir-Az left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verified on Windows 11 with a D455 under a de-DE region: JSON round-trip truncates without the fix and is correct with it.

@Nir-Az

Nir-Az commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

ok to test

@sysrsbuild-gh

Copy link
Copy Markdown

Can one of the admins verify this patch?

@Nir-Az
Nir-Az merged commit 7015139 into realsenseai:development Oct 28, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

⌂ 换网址