← Back to Pull requests

fix: Parse JSON doubles with strtod_l wherever libc++ lacks from_chars #462

Closed
unknown closed this pull request without merging on Nov 20, 2026 22:54 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 18:29 UTC
Author

refs: RUM-20302

## Context

We want the code in types to be portable to other platforms and build toolchains, even those where dd-sdk-cpp is 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.cpp contains code for converting JSON-encoded strings to various primitive types, including double.

Ordinarily, we can just use std::from_chars with std::chars_format::general (which corresponds to %g). This parses all valid JSON number values as intended:

bool ParseJsonDouble(std::string_view json_literal, double& out) {
  const char* begin = json_literal.data();
  const char* end = begin + json_literal.size();
  auto [ptr, ec] = std::from_chars(begin, end, out, std::chars_format::general);
  return ec == std::errc{} && ptr == end;
}

However, while std::from_chars was added in C++17, the specific overloads for floating-point parsing are not supported in all C++ standard library implementations. For example, Apple's libc++ only enables them when targeting macOS 26 or newer. Therefore, our routine for parsing floating-point JSON values actually looks like this:

bool ParseJsonDouble(std::string_view json_literal, double& out) {
#ifdef PARSE_JSON_DOUBLE_WITH_STRTOD
  // Fallback for platforms without floating-point from_chars (e.g. Apple < macOS 15).
  // std::to_chars on the encoding side always writes 'C'-locale decimal (.), so
  // strtod_l with an explicit 'C' locale is a safe and interchangeable substitute,
  // regardless of the process LC_NUMERIC setting.
  static const locale_t kCLocale = newlocale(LC_NUMERIC_MASK, "C", nullptr);
  const std::string tmp(json_literal);
  char* end_ptr = nullptr;
  errno = 0;
  const double val = strtod_l(tmp.c_str(), &end_ptr, kCLocale);
  if (errno != 0 || end_ptr != tmp.c_str() + tmp.size()) {
    return false;
  }
  out = val;
  return true;
#else
  // <The same std::from_chars implementation quoted above>
#endif
}

This is the status quo: this conditional-compilation branch exists today. This PR concerns how we determine whether
PARSE_JSON_DOUBLE_WITH_STRTOD is enabled.

The existing approach checked #if defined(__APPLE__) && (!defined(__cpp_lib_to_chars) || __cpp_lib_to_chars < 201611L), using the strtod_l fallback 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_STRTOD has a few issues:

  • It causes the SDK to fail to compile on Linux with libc++ 19 or older, since the fallback path is gated by __APPLE__ (libstdc++ was still fine)

  • It means that the SDK will fail to compile on Android with NDK r28 and older

  • Less critically: on macOS and iOS it compiles the fallback rather than using 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_STRTOD to:

    #if defined(_LIBCPP_VERSION) &&                                      \
        (_LIBCPP_VERSION < 200000 ||                                     \
         (defined(_LIBCPP_AVAILABILITY_HAS_FROM_CHARS_FLOATING_POINT) && \
          !_LIBCPP_AVAILABILITY_HAS_FROM_CHARS_FLOATING_POINT))
    

    Here's how the decision breaks down, in practice:

  • Are we using libc++?

  • - If yes _(macOS, iOS, Android, Linux iff using Clang/libc++)_: - Is our libc++ version 20.0 or newer? - If yes: we use std::from_chars if _LIBCPP_AVAILABILITY_HAS_FROM_CHARS_FLOATING_POINT; strtod_l otherwise - If no: we use strtod_l - In practice, we use std::from_chars with 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_chars unconditionally

    libc++ releases older than 20 don't define
    _LIBCPP_AVAILABILITY_HAS_FROM_CHARS_FLOATING_POINT at 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_l unconditionally, there are a few reasons to preserve the std::from_chars path where we can help it. Basically, strtod_l:

  • Is a non-standard extension, so we can't use it on Windows as written

  • Requires a null-terminated string, so we have to copy into a temporary std::string that 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_l fallback rejects subnormal values (e.g. 5e-324), where std::from_chars parses them correctly.

    strtod_l sets ERANGE for subnormal results as well as for overflow and underflow to zero, and our code treated any nonzero errno as failure. Since WriteJson writes subnormals like 5e-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 ParseJsonDouble tests cover subnormal values, a round trip of the smallest subnormal through WriteJson, 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 to strtod_l. Both paths pass all tests.

    parse_primitives.cpp compiles with NDK r27c and r28 for arm64-v8a and x86_64 at API level 26, selecting strtod_l, and on-device parsing checks passed on Android and iOS. NDK r29 (libc++ 21) and LLVM 20.1's libc++ headers select std::from_chars. With Xcode 26.4, deployment targets of macOS 14 and 15 and iOS 15 select strtod_l, and targets of macOS 26 and iOS 26 select std::from_chars.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: fuzzybinary Review duration: 264m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%