fix: Parse JSON doubles with strtod_l wherever libc++ lacks from_chars #462
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #462"
general
weight: 0.35
fuzzybinary
refs: RUM-20302
## Context
We want the code in
typesto be portable to other platforms and build toolchains, even those wheredd-sdk-cppis not supported in full. This is one in a series of PRs addressing minor portability issues.This one deals with how we parse floating-point values from JSON.
## Problem
There are some cases where the SDK needs to employ some limited JSON parsing, and we implement our own parsing code rather than using a third-party library.
json/parse_primitives.cppcontains code for converting JSON-encoded strings to various primitive types, includingdouble.Ordinarily, we can just use
std::from_charswithstd::chars_format::general(which corresponds to%g). This parses all valid JSON number values as intended:However, while
std::from_charswas added in C++17, the specific overloads for floating-point parsing are not supported in all C++ standard library implementations. For example, Apple'slibc++only enables them when targeting macOS 26 or newer. Therefore, our routine for parsing floating-point JSON values actually looks like this:This is the status quo: this conditional-compilation branch exists today. This PR concerns how we determine whether
PARSE_JSON_DOUBLE_WITH_STRTODis enabled.The existing approach checked
#if defined(__APPLE__) && (!defined(__cpp_lib_to_chars) || __cpp_lib_to_chars < 201611L), using thestrtod_lfallback if true. libc++ never actually defines__cpp_lib_to_chars, so the fallback was effectively just globally enabled on all Apple builds.Therefore, the existing check for
PARSE_JSON_DOUBLE_WITH_STRTODhas a few issues:__APPLE__(libstdc++ was still fine)std::from_chars, even with a deployment target of 26.0 or newer## Solution
This PR updates the condition for defining
PARSE_JSON_DOUBLE_WITH_STRTODto:Here's how the decision breaks down, in practice:
- If yes _(macOS, iOS, Android, Linux iff using Clang/libc++)_: - Is our libc++ version 20.0 or newer? - If yes: we use
std::from_charsif_LIBCPP_AVAILABILITY_HAS_FROM_CHARS_FLOATING_POINT;strtod_lotherwise - If no: we usestrtod_l- In practice, we usestd::from_charswith LLVM 20.1 or newer, NDK r29 or newer, and Apple deployment targets of macOS or iOS 26 or newer- If no _(Windows, Linux with GCC, Linux with Clang/libstdc++)_:
- We use
std::from_charsunconditionallylibc++ releases older than 20 don't define
_LIBCPP_AVAILABILITY_HAS_FROM_CHARS_FLOATING_POINTat all, so the version check is what catches them.The preprocessor logic is a bit hairy, so I tried to annotate it with a comment that clearly outlines the practical implications on each platform.
While it would be tempting to get rid of this extra conditional-compilation complexity and just use
strtod_lunconditionally, there are a few reasons to preserve thestd::from_charspath where we can help it. Basically,strtod_l:std::stringthat might allocate (although only for large values if SSO does its job)## Additional fixes
I noticed a minor bug while working on this change: the
strtod_lfallback rejects subnormal values (e.g.5e-324), wherestd::from_charsparses them correctly.strtod_lsetsERANGEfor subnormal results as well as for overflow and underflow to zero, and our code treated any nonzeroerrnoas failure. SinceWriteJsonwrites subnormals like5e-324, those values couldn't round-trip on any build that uses the fallback, including our macOS release builds. The fallback now accepts subnormal results and still rejects overflow, underflow to zero, and other errors.## Testing
New
ParseJsonDoubletests cover subnormal values, a round trip of the smallest subnormal throughWriteJson, and rejection of overflow and underflow to zero.These tests were run on macOS, first in a build that uses
std::from_chars, and then in a build that targets macOS 14.0 and so falls back tostrtod_l. Both paths pass all tests.parse_primitives.cppcompiles with NDK r27c and r28 for arm64-v8a and x86_64 at API level 26, selectingstrtod_l, and on-device parsing checks passed on Android and iOS. NDK r29 (libc++ 21) and LLVM 20.1's libc++ headers selectstd::from_chars. With Xcode 26.4, deployment targets of macOS 14 and 15 and iOS 15 selectstrtod_l, and targets of macOS 26 and iOS 26 selectstd::from_chars.