Skip to content

fix(libs): parse decimals on macOS releases without floating from_chars - #27767

Closed
LODIBIDO wants to merge 2 commits into
boykopovar:mainfrom
LODIBIDO:fix/macos-floating-from-chars
Closed

LODIBIDO wants to merge 2 commits into
boykopovar:mainfrom
LODIBIDO:fix/macos-floating-from-chars

Conversation

@LODIBIDO

@LODIBIDO LODIBIDO commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What

On macOS releases before 26, libc++ has no floating-point std::from_chars, so libSceNKWebKit/CharactersToNumber.cpp and libSceVideoOut/src/InputMapping.cpp do not compile (call to deleted function 'from_chars').

Both now call ParseDecimal_nid_no_patch in the new core/libs/DecimalParsing.hpp.

  • Every other target, including macOS 26 and later, calls std::from_chars exactly as before.
  • Before macOS 26 it scans the from_chars general syntax (decimal numbers, inf, infinity, nan, nan(...)), converts with strtod_l or strtof_l in the C locale under the default floating-point environment, restores that environment and errno, and reports overflow and underflow to zero as result_out_of_range without writing the value.

The guard is the one in RavenCurseBurg's candidate fix for UpscaleMode.hpp (744123f, posted on #24341). This PR does not touch that file, so libSceAgcDriver still needs that fix on these releases.

#20016 also edits InputMapping.cpp; the from_chars call it adds parses an integer.

Refs #27647.

Tested

Native Intel Mac (i5-8259U, x86_64, no Rosetta, macOS 15.7.8, Apple clang 17.0.0), Release, main afdd339 plus the diff of #27201 so that libc compiles.

  • Without this change both source files fail with the error above; with it they compile.
  • pad_input_mapping, guest_nkwebkit_numbers, nkwebkit_wtf, nkwebkit_wtftext, nkwebkit_wtftext_allocation, nkwebkit_doubletofixed and nkwebkit_doubletostringconverter: each passed 5 of 5. pad_input_mapping has new cases for rejected syntax, range limits and a written zero.
  • A throwaway program compared the new path with exact rational arithmetic on 63,064 strings, for float and double: no difference in error code, consumed length or result bits (for NaN: sign and NaN class, not the payload), by default, with a comma-decimal locale, with tr_TR.UTF-8 and tr_TR.ISO8859-9, and with downward rounding and flush-to-zero/denormals-are-zero set. errno, the rounding mode and MXCSR were unchanged after each call.
  • A syntax-only pass over the 74 configured translation units that use from_chars: the only from_chars errors left are in VulkanDevice.cpp and PresentationPass.cpp, both from UpscaleMode.hpp:25. libSceAgcDriver/tests/Recorder.cpp needs generated shader headers that were not built; with placeholder headers it reports no from_chars error. The pass also reports an unrelated error on main, libSceAgcDriver/Execution/src/GuestMemory.cpp:4175 (no matching function for call to 'min').
  • Overlap was checked from the file lists of open pull requests; tools/pr_overlap.py was not run.

Not tested: Linux, Windows, Apple silicon, macOS 26, any title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (only the input settings parser changes in VideoOut; no GPU test run)
  • AI-assisted: yes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved decimal-number parsing on supported macOS versions, including values with exponents and signed infinity or NaN.
    • Numeric settings now handle written zero and report malformed or out-of-range values more consistently, including overflow and underflow cases.
    • Decimal values entered in browser-related and video settings now use consistent parsing behavior.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Decimal parsing

Layer / File(s) Summary
Add decimal parsing helper
core/libs/DecimalParsing.hpp
Adds ParseDecimal_nid_no_patch. On macOS deployment targets below 26.0, it parses floating-point values and reports invalid or out-of-range input. On other targets, it delegates to std::from_chars.
Use helper in WebKit conversion
core/libs/prx/libSceNKWebKit/CharactersToNumber.cpp
Convert uses the helper to parse its generated decimal string. Existing result handling remains unchanged.
Use helper for VideoOut settings
core/libs/prx/libSceVideoOut/src/InputMapping.cpp, core/libs/prx/libSceVideoOut/tests/InputMapping.cpp
parseMouseSensitivity and parseNumber use the helper. Tests cover exponent values, zero, and malformed or out-of-range inputs.

Possibly related issues

Possibly related PRs

  • boykopovar/AnyPS5#24669: Addresses the same macOS floating-point std::from_chars build failure in a different library using std::strtod.

Suggested reviewers: privacyonion

Change: Bug fix

Merge Risk: Minimal · up to c2aeb

This change lets two decimal-parsing call sites compile and behave consistently on older macOS releases. No remaining defects were identified, and other platforms continue to use the standard library path.

Security architecture risk: Minimal · up to c2aeb

The compatibility fallback runs behind the existing WebKit and input-settings entrypoints. It preserves their validation and rejection rules, and an invalid settings reload still leaves the active mapping in place. The reviewed change establishes no material increase in security risk.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The inspected exposure is limited to existing guest number conversion and host input-mapping file parsing. Guest input reaches the fallback only after normalization and a 772-significant-digit cap, with at most one extra digit to represent discarded nonzero digits and a generated exponent.

Trust Boundaries and Controls

  • observed — The fallback passes only the scanned token to the C conversion routines. It saves the calling thread’s floating-point environment and errno, runs conversion under the default environment, and calls restoration before returning either a value or a range error.

Resilience and Maintainability Implications

  • observed — Input-mapping reload builds a candidate before publication. An ordinary parser rejection keeps the prior snapshot active; a successful load replaces it under the existing mutex. Neither the manual reload nor the file watcher changes that order.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 657f972b-1189-461f-b08a-4dd2a2ff1018
Commits

Reviewing files that changed from the base of the PR and between ece6946 and c2aeb13.

Files selected for processing: 1

  • core/libs/DecimalParsing.hpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@JJenkx JJenkx 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 fallback scans its special tokens in the caller's character locale before converting in the C locale. strncasecmp uses the current locale on macOS, and std::isalnum also uses the current character locale. Floating-point from_chars requires C-locale syntax for the whole scan. Apple libc · C++ floating-point grammar

I forced the exact fallback header at ece6946fc027 on Linux with a small adapter for LC_C_LOCALE, then compared with the ordinary std::from_chars branch. With tr_TR.UTF-8, INF, INFINITY, and -InFiNiTy! returned invalid_argument for both float and double when the ordinary branch accepted them. With tr_TR.ISO-8859-9, a NaN followed by a parenthesized non-ASCII letter also reported 6 bytes consumed where the C-locale parser consumed 3.

A local variant using strncasecmp_l(..., LC_C_LOCALE) and isalnum_l(..., LC_C_LOCALE) matched all 102 comparisons across C and both Turkish locales. Please make the character scan locale-independent as well. This checks the scanner contract. The two current callers reject or never generate these special tokens. The forced-branch test used Linux libc and did not run on macOS.

@MarcZX10 MarcZX10 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.

Reviewed — no blocking findings

The reviewed change has no verified blocking defects; the reported macOS tests were not run in this review.

What I ran

Read the prepared diff and all four changed files at head ece6946fc02790fa3cad337be239cacb96b44b15 through repo_read.py. No build or test commands were run.

Results

Check Result Note
Changed code review passed All four changed files read
Build and tests not run Results in the PR description are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build and test results were not independently verified. Linux, Windows, Apple silicon, macOS 26, and console behavior were not tested in this review. All files listed as reviewed were read: core/libs/DecimalParsing.hpp, core/libs/prx/libSceNKWebKit/CharactersToNumber.cpp, core/libs/prx/libSceVideoOut/src/InputMapping.cpp, and core/libs/prx/libSceVideoOut/tests/InputMapping.cpp. No changed files were excluded.

To get this to a good state

Nothing is required.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@LODIBIDO

Copy link
Copy Markdown
Contributor Author

Thank you, you are right. I reproduced it natively on Intel macOS 15.7.8 at ece6946fc0: with tr_TR.UTF-8 and tr_TR.ISO8859-9, my comparison against exact expected results showed 4 mismatches in each, including nan( followed by a non-ASCII letter consuming the parentheses.

The scanner now compares inf, infinity and nan as ASCII and checks the nan(...) characters by explicit ASCII ranges, so <cctype> and <strings.h> are gone. The same comparison now reports no mismatch on 63,064 strings, for float and double, in both Turkish locales, and the seven tests listed in the description each pass 5 of 5.

AI-assisted: yes

@MarcZX10 MarcZX10 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.

Reviewed — changes requested

Changes are required before merge: restore the saved floating-point environment even when conversion or classification fails.

What I ran

Reviewed head c2aeb13ac63d603aacf2f5e7ef1cdb689a6b1884 and its prepared diff. I read all four changed files listed in the manifest; no commands or tests were run.

Results

Check Result Note
Diff and changed code passed Four changed files read
Tests not run No local execution

Findings

Blocking

  • core/libs/DecimalParsing.hpp:68 changes the calling thread’s floating-point environment to FE_DFL_ENV, then restores it only on the normal path at line 73. If std::string allocation at line 64 throws, or a conversion/classification operation fails exceptionally, the caller’s rounding mode and exception flags may remain altered. Guard the environment change with an RAII restore so every exit path reinstates it.

Non-blocking

None.

Not verified

No tests or compilation were run. The PR description reports author-run macOS checks, but these were not independently verified. No files were excluded by the provided manifest; all four changed files were read.

To get this to a good state

Add exception-safe restoration of the floating-point environment, then rerun the relevant parsing and environment-preservation checks.

@JJenkx JJenkx 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.

I retested the ASCII scanner on exact head c2aeb13ac63d. The forced fallback matched std::from_chars in all 102 comparisons across C, Turkish UTF-8, and Turkish ISO-8859-9.

For the floating-point environment follow-up, 72 checks passed across all four rounding modes. They cover successful, invalid, overflow, and underflow inputs, plus eight injected std::bad_alloc throws from the temporary string allocation for float and double. The caller's rounding mode and pre-raised exception flags survived all cases. The allocation happens before the switch to FE_DFL_ENV, and the ordinary out-of-range return follows the restore. This experiment used the exact fallback on Linux with a C-locale API adapter. Native macOS remains the portability check.

AI-assisted: yes

@LODIBIDO

Copy link
Copy Markdown
Contributor Author

@MarcZX10 I checked the order at c2aeb13ac6 and I don't see an exit path that leaves the environment changed.

  • The std::string is constructed at line 64, before std::fegetenv (line 67) and std::fesetenv(FE_DFL_ENV) (line 68). If that allocation throws, the floating-point environment and errno have not been touched yet.
  • Between line 68 and the restore at line 73 there are only strtof_l or strtod_l, which are C library functions and report errors through their result and errno, and std::fpclassify. None of them can throw a C++ exception, and the block has no early return.

So every path that changes the environment also reaches std::fesetenv(&environment). I would rather not add a guard class for a path that does not exist, but I will if you still want it. Could you take another look?

AI-assisted: yes

@NobleBrains

Copy link
Copy Markdown
Contributor

Tested on Apple silicon: Apple M4, macOS 15.7.9, Apple clang 17.0.0, CMAKE_OSX_ARCHITECTURES=x86_64 (Rosetta), Release, BUILD_TESTING=ON. Tree: main at 6f6f6c8e6b plus an unrelated libSceNpManager change, with the diffs of #27201, #27739, #27843, #27767, #17425 and #27827 applied; with all six the full build finished without errors.

For this PR (at c2aeb13ac6): before it, libSceVideoOut/src/InputMapping.cpp and libSceNKWebKit/CharactersToNumber.cpp did not compile (from_chars unavailable); with it, both do, and pad_input_mapping and nkwebkit_wtftext_allocation passed.

AI-assisted: yes (Claude Code)

@ligmaSec ligmaSec 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.

pad_input_mapping and agc_driver_gcn_scalar_buffer_base passed on main and the patched tree on the same Linux llvmpipe device with Vulkan required, shader cache disabled, and validation layer 1.4.369. Both logs show layer insertion and no missing-layer diagnostic or VUID- entry. A Linux fixture that selected the macOS fallback passed 20,052 conversion comparisons and caller-state checks. Native macOS remains outside this run.

AI-assisted: yes.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

I read DecimalParsing.hpp at c2aeb13 against MarcZX10's change request. The std::string text(first, end) allocation is declared before fegetenv/fesetenv, so a bad_alloc cannot leave the default environment set. After fesetenv(FE_DFL_ENV) the only calls are strtof_l/strtod_l, fpclassify and fesetenv, none of which throw, so the restore on the normal path runs on every exit that can occur. An RAII guard would not change behaviour. The macOS branch only compiles for deployment targets below 26, so Linux and Windows still use std::from_chars, and the new InputMapping tests cover both.

@oneandonlydean
oneandonlydean dismissed MarcZX10’s stale review December 3, 2026 15:11

The ask was an RAII restore of the floating point environment. In DecimalParsing.hpp the std::string is built at line 64, before fegetenv and fesetenv (lines 67-68). Between that and the restore at line 73 only strtof_l or strtod_l and std::fpclassify run, which cannot throw and have no early return, so every path restores it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants