fix(libc): store a scanned NaN as the default quiet NaN in sscanf_s #12532

Merged
JJenkx merged 2 commits from upstream-pr/12532 into main 2026-11-06 13:58:12 +00:00
JJenkx commented 2026-10-28 17:59:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-28T17:59:31Z.


Current-main integration — 2026-11-05. Exact head 5c3cff5ecd2e5128aab2ac390649dd9da4b0b330 retains previous public 3e78245d86df6a16aadd1d8b96b494a6744d0fa8 and tested main 6d173c1a19788ff69d43b3d0ca62833e312ef939 as ancestors. The bounded native-host NaN read and explicit ten-byte guest x87 NaN helper remain byte-identical to the previous public head, including padding tests. Actual current #12530 parent af0443d37194a7000804f22c1e71c82cae572dbd is retained by a history-only merge with an unchanged source tree. No full eight-byte-host long-double library port is claimed.

Linux x86-64, GCC16.2.1, Ninja Release: actual production modules and focused fixture owners rebuilt; 3/3 runtime tests pass, zero skips: guest_formatting, guest_stdio_variadic, guest_scan_count. Matching pinned main passes all25 selected baseline groups. Production NID patching, conventions/whitespace, and MinGW syntax for Formatting.cpp pass.

Public-header audit: failed on both pinned main and this head with the same five missing WKSecurityOrigin* declarations (Create, CopyToString, CopyHost, CopyProtocol, GetPort), with no additional declaration errors. The failure is retained in the logs; no all-green header/full-suite claim is made. The concrete shared-main repair is #16010, exact head 545e24b2ee2e549001421f7fc9f87ff801f3423c. Its independent qualification reports a green audit of 406 libraries/30,572 exports and two standalone units compiled by each of GCC and MinGW. A maintainer still needs to merge that repair and rerun the affected checks; this unmodified qualification tree is not relabelled header-green.

The older-main Windows DepthSurface::createSampleShadow allocation compile error is already corrected by merged #15855. This head contains that fix, and its DepthSurface.cpp/ImageMemory.hpp blobs match the fixing source. The fix’s hosted Windows build passed; that is attributed upstream evidence, not a Windows build/runtime result for this head. No duplicate AGC change was added. A final merge preview against newer main 7ad9749ed0311cc91d69bc7c690d09c92b9d42d0 is clean; qualification remains pinned to 6d173c1a19788ff69d43b3d0ca62833e312ef939.

Source/parent/provider/gitlink proofs, commands, raw logs, JUnit, hashes and known baseline failure. Historical platform, module-reference and performance reports below retain their original pins; no new Windows/macOS, SDK/physical-console, title/GPU or performance run.

Earlier pinned reports and the contribution template follow.

Refreshed against main 0263899e996f26b165aed37f6b5a616dd1d4c6bb; current head 3e78245d86df6a16aadd1d8b96b494a6744d0fa8 retains previous public head 6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c and main as ancestors. The conflict resolution preserves the topic behavior and current-main ordinary-scan/input-failure handling and fixtures.

The same five fixtures (guest_formatting, guest_scan_count, guest_wide_stream_scan, guest_wide_formatting, guest_crt) pass three times on main and on this head: 15 executions per arm, zero failures/skips. Public-header audit, conventions and whitespace checks pass. These are actual scoped libc/kernel/Ampr owners and selected fixtures with existing frozen ZArchive/zstd providers, not a whole-project build. No new Windows, title-unblock or performance claim.

Raw build/test/JUnit logs, commands, source patches, conflict resolutions and binary hashes, SHA-256 fd8ddb6a38a010916d71698df9cb60011e9800fe14db63baa9f4eca21e6b8e4f (download verified). The batch has 87 successful fixture executions in total; setup failures and corrected final qualification are retained in the archive.

Previous report and contribution template, with historical pins preserved:

Stacked on #12530, which is on #12507. Current head 6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c includes the helper correction and main integration; the original subject commit 92c6891d1c is historical.

What

sscanf_s and vsscanf_s now store every NaN a floating conversion reads as the positive default quiet NaN, as the libc.prx that titles ship does. The host scanners keep the sign of -nan, so main stored a negative NaN.

input width titles (PPSA07923, PPSA05916) main
-nan, -NaN, -nan(0x5) %f 0x7fc00000 0xffc00000
same %lf 0x7ff8000000000000 0xfff8000000000000
same %Lf 0x7fff:c000000000000000 0xffff:c000000000000000

nan, NAN, +nan, nan(1) and nan() already gave the positive default NaN.

After the host has stored its native value, the scanner classifies it within that value’s width. A %Lf NaN is written as ten explicit guest x87 quiet-NaN bytes, leaving the trailing padding untouched; other widths use std::numeric_limits<T>::quiet_NaN(). This also covers the stream forms (fscanf_s, scanf_s), because it runs after the store.

This is the same rule #12514 measured for swscanf. Plain sscanf takes a separate path (glibc on Linux, WindowsScanning.hpp on Windows) and the titles behave the same there, but it isn't changed here. It is part of the narrow-scanner work planned after #9087.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, head 92c6891d1c.

  • Native comparison: a scratch loader calls the sscanf_s of PPSA07923 and PPSA05916 by NID.
    • NaN inputs: 8 inputs (nan, -nan, NAN, -NaN, nan(1), -nan(0x5), nan(), +nan) at all three widths: 0 differences (9 before).
    • Random: the 200,000-conversion random probe from #12530 drops from 1 difference per title (the %5Lf of "-naNXf") to 0.
  • Unit tests: guest_formatting checks five NaN spellings at each width. With #12530's Formatting.cpp, it fails.
  • Regression set: guest_formatting, guest_stdio_variadic, guest_scan_count, guest_wide_stream_scan, guest_wide_formatting, file_streams and the five guest_standard_streams* tests pass 3/3. Conventions pass.

Current Linux qualification — 2026-10-29

Addressed the helper’s 8-byte-host overflow and the padding-test suggestion in 8f74e7ad1d7. After successful host scanning, NaN classification reads the native host value width (bounded by the requested value bytes). A NaN in the %Lf path is emitted as ten explicit guest x87 quiet-NaN bytes; other widths and finite conversions are unchanged. The regression now checks that bytes10–15 remain untouched.

Linux Release: guest_formatting, guest_stdio_variadic and guest_scan_count pass three times (9 checks), then all9 pass after restoration. A production mutant writing16bytes instead of10 fails the new padding assertion; restored libc is byte-identical. An ASan+UBSan harness extracted from the actual helper reproduces the parent’s stack over-read with GCC -mlong-double-64; the correction passes both that helper-only ABI model and native x87 checks, including exact NaN bits, finite preservation and tail guards. Conventions and whitespace pass.

The caller writes host scanf output before this helper runs; the helper is not parsing an existing guest80 value on an8-byte host. The full library’s x87 requirement remains unchanged: this does not establish a full8-byte-long-double library/ARM64/macOS port, and no new Windows or title-library comparison is claimed. AI-assisted: yes.

Current-main integration: normal fast-forward merge 6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c now includes main 157b02c4f1803e9d2cd757677b9a805961203fc4. The only conflict was <bit> versus ScanQuadLength.hpp; both includes and every nonblank added test line from main and this stack are retained. The NaN helper is byte-identical to the qualified correction, and the actual #12530 ancestry remains intact. All three focused groups pass three repetitions on this integrated source (9 more passes), with conventions and whitespace clean.

Evidence: immutable Linux qualification bundle. Its SHA-256 is ca6b35926bcc6d3f21a9650517894c86b65594e2e2b653c5c32cd1c1de89c835. No fresh Windows runtime execution is claimed; older measurements retain their recorded source pins.

AI-assisted: yes (Codex).

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
  • 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: #12530 (actual current parent af0443d37194a7000804f22c1e71c82cae572dbd)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5055253841 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12532). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T17:59:31Z. --- <!-- cpu-main6d173-refresh-20261105-12532 --> **Current-main integration — 2026-11-05.** Exact head `5c3cff5ecd2e5128aab2ac390649dd9da4b0b330` retains previous public `3e78245d86df6a16aadd1d8b96b494a6744d0fa8` and tested main `6d173c1a19788ff69d43b3d0ca62833e312ef939` as ancestors. The bounded native-host NaN read and explicit ten-byte guest x87 NaN helper remain byte-identical to the previous public head, including padding tests. Actual current #12530 parent `af0443d37194a7000804f22c1e71c82cae572dbd` is retained by a history-only merge with an unchanged source tree. No full eight-byte-host long-double library port is claimed. Linux x86-64, GCC16.2.1, Ninja Release: actual production modules and focused fixture owners rebuilt; **3/3 runtime tests pass, zero skips**: `guest_formatting`, `guest_stdio_variadic`, `guest_scan_count`. Matching pinned main passes all25 selected baseline groups. Production NID patching, conventions/whitespace, and MinGW syntax for `Formatting.cpp` pass. **Public-header audit: failed on both pinned main and this head with the same five missing `WKSecurityOrigin*` declarations** (`Create`, `CopyToString`, `CopyHost`, `CopyProtocol`, `GetPort`), with no additional declaration errors. The failure is retained in the logs; no all-green header/full-suite claim is made. The concrete shared-main repair is [#16010](https://github.com/boykopovar/AnyPS5/pull/16010), exact head `545e24b2ee2e549001421f7fc9f87ff801f3423c`. Its independent qualification reports a green audit of 406 libraries/30,572 exports and two standalone units compiled by each of GCC and MinGW. A maintainer still needs to merge that repair and rerun the affected checks; this unmodified qualification tree is not relabelled header-green. The older-main Windows `DepthSurface::createSampleShadow` allocation compile error is already corrected by merged [#15855](https://github.com/boykopovar/AnyPS5/pull/15855). This head contains that fix, and its DepthSurface.cpp/ImageMemory.hpp blobs match the fixing source. The fix’s [hosted Windows build passed](https://github.com/boykopovar/AnyPS5/actions/runs/41901908835/job/126840906295); that is attributed upstream evidence, not a Windows build/runtime result for this head. No duplicate AGC change was added. A final merge preview against newer main `7ad9749ed0311cc91d69bc7c690d09c92b9d42d0` is clean; qualification remains pinned to `6d173c1a19788ff69d43b3d0ca62833e312ef939`. [Source/parent/provider/gitlink proofs, commands, raw logs, JUnit, hashes and known baseline failure](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/cpu5-main6d173-linux-20261105-terminal.tar.gz). Historical platform, module-reference and performance reports below retain their original pins; no new Windows/macOS, SDK/physical-console, title/GPU or performance run. Earlier pinned reports and the contribution template follow. <!-- codex-format-main026-20261104 --> Refreshed against main `0263899e996f26b165aed37f6b5a616dd1d4c6bb`; current head `3e78245d86df6a16aadd1d8b96b494a6744d0fa8` retains previous public head `6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c` and main as ancestors. The conflict resolution preserves the topic behavior and current-main ordinary-scan/input-failure handling and fixtures. The same five fixtures (`guest_formatting`, `guest_scan_count`, `guest_wide_stream_scan`, `guest_wide_formatting`, `guest_crt`) pass three times on main and on this head: 15 executions per arm, zero failures/skips. Public-header audit, conventions and whitespace checks pass. These are actual scoped libc/kernel/Ampr owners and selected fixtures with existing frozen ZArchive/zstd providers, not a whole-project build. No new Windows, title-unblock or performance claim. [Raw build/test/JUnit logs, commands, source patches, conflict resolutions and binary hashes](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-libc-format4-main026-linux-20261104.tar.gz), SHA-256 `fd8ddb6a38a010916d71698df9cb60011e9800fe14db63baa9f4eca21e6b8e4f` (download verified). The batch has 87 successful fixture executions in total; setup failures and corrected final qualification are retained in the archive. Previous report and contribution template, with historical pins preserved: **Stacked on #12530, which is on #12507.** Current head `6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c` includes the helper correction and main integration; the original subject commit `92c6891d1c` is historical. ### What `sscanf_s` and `vsscanf_s` now store every NaN a floating conversion reads as the positive default quiet NaN, as the `libc.prx` that titles ship does. The host scanners keep the sign of `-nan`, so `main` stored a negative NaN. | input | width | titles (PPSA07923, PPSA05916) | `main` | |---|---|---|---| | `-nan`, `-NaN`, `-nan(0x5)` | `%f` | `0x7fc00000` | `0xffc00000` | | same | `%lf` | `0x7ff8000000000000` | `0xfff8000000000000` | | same | `%Lf` | `0x7fff:c000000000000000` | `0xffff:c000000000000000` | `nan`, `NAN`, `+nan`, `nan(1)` and `nan()` already gave the positive default NaN. After the host has stored its native value, the scanner classifies it within that value’s width. A `%Lf` NaN is written as ten explicit guest x87 quiet-NaN bytes, leaving the trailing padding untouched; other widths use `std::numeric_limits<T>::quiet_NaN()`. This also covers the stream forms (`fscanf_s`, `scanf_s`), because it runs after the store. This is the same rule #12514 measured for `swscanf`. Plain `sscanf` takes a separate path (glibc on Linux, `WindowsScanning.hpp` on Windows) and the titles behave the same there, but it isn't changed here. It is part of the narrow-scanner work planned after #9087. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, head `92c6891d1c`. - **Native comparison:** a scratch loader calls the `sscanf_s` of PPSA07923 and PPSA05916 by NID. - **NaN inputs:** 8 inputs (`nan`, `-nan`, `NAN`, `-NaN`, `nan(1)`, `-nan(0x5)`, `nan()`, `+nan`) at all three widths: 0 differences (9 before). - **Random:** the 200,000-conversion random probe from #12530 drops from 1 difference per title (the `%5Lf` of `"-naNXf"`) to **0**. - **Unit tests:** `guest_formatting` checks five NaN spellings at each width. With #12530's `Formatting.cpp`, it fails. - **Regression set:** `guest_formatting`, `guest_stdio_variadic`, `guest_scan_count`, `guest_wide_stream_scan`, `guest_wide_formatting`, `file_streams` and the five `guest_standard_streams*` tests pass 3/3. Conventions pass. <!-- codex-maintenance-20261029-repeat-12532 --> **Current Linux qualification — 2026-10-29** Addressed the helper’s 8-byte-host overflow and the padding-test suggestion in `8f74e7ad1d7`. After successful host scanning, NaN classification reads the native host value width (bounded by the requested value bytes). A NaN in the `%Lf` path is emitted as ten explicit guest x87 quiet-NaN bytes; other widths and finite conversions are unchanged. The regression now checks that bytes10–15 remain untouched. Linux Release: `guest_formatting`, `guest_stdio_variadic` and `guest_scan_count` pass three times (9 checks), then all9 pass after restoration. A production mutant writing16bytes instead of10 fails the new padding assertion; restored libc is byte-identical. An ASan+UBSan harness extracted from the actual helper reproduces the parent’s stack over-read with GCC `-mlong-double-64`; the correction passes both that helper-only ABI model and native x87 checks, including exact NaN bits, finite preservation and tail guards. Conventions and whitespace pass. The caller writes **host scanf output** before this helper runs; the helper is not parsing an existing guest80 value on an8-byte host. The full library’s x87 requirement remains unchanged: this does not establish a full8-byte-long-double library/ARM64/macOS port, and no new Windows or title-library comparison is claimed. AI-assisted: yes. Current-main integration: normal fast-forward merge `6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c` now includes `main` `157b02c4f1803e9d2cd757677b9a805961203fc4`. The only conflict was `<bit>` versus `ScanQuadLength.hpp`; both includes and every nonblank added test line from main and this stack are retained. The NaN helper is byte-identical to the qualified correction, and the actual #12530 ancestry remains intact. All three focused groups pass three repetitions on this integrated source (9 more passes), with conventions and whitespace clean. Evidence: [immutable Linux qualification bundle](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/cpu-complete-current-main-linux-20261029.tar.gz). Its SHA-256 is `ca6b35926bcc6d3f21a9650517894c86b65594e2e2b653c5c32cd1c1de89c835`. No fresh Windows runtime execution is claimed; older measurements retain their recorded source pins. AI-assisted: yes (Codex). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #12530 (actual current parent `af0443d37194a7000804f22c1e71c82cae572dbd`) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-10-28 18:23:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T18:23:14Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

On hosts where long double is 8 bytes, the new %Lf NaN normalization copies 10 bytes from an 8-byte temporary, which AddressSanitizer reports as a stack-buffer-overflow; it also does not encode the expected x87 80-bit quiet NaN.

What I ran

Reviewed head 92c6891d1cdec64888aac01dbabdcea24b97101e against its declared base e4da19aff06e25ae16b90f460dff22e8ac1eb786; the cumulative PR merges cleanly onto current main at f21ef7c99c5af648c5a41bddb18ce118b8e60c46. Environment: Debian 12 x86_64, GCC 12.2. git diff --check passed. I also compiled the new helper behavior with GCC’s -mlong-double-64 mode to model the 8-byte long double ABI noted for macOS ARM in this PR’s own documentation.

git merge-tree $(git merge-base upstream/main pr/12532) upstream/main pr/12532
git diff --check upstream/main...pr/12532
g++ -std=c++20 -mlong-double-64 -fsanitize=address -g /tmp/pr12532_ld80_probe.cpp -o /tmp/pr12532_ld80_probe
/tmp/pr12532_ld80_probe

The probe source applies the PR’s StoreDefaultNaN<T> implementation to a 16-byte guest buffer whose first eight bytes contain a negative binary64 NaN, with StoreDefaultNaN<long double>(guest, 10). The address sanitizer reports the 10-byte read from the 8-byte local value.

Results

Check Result
Merge onto current main Clean
git diff --check Pass; no output
8-byte long double ASan probe Fails with stack-buffer-overflow at the memcpy(target, &value, bytes) operation
Non-ASan probe output mantissa=7ff8000000000000 exponent=b2a0 sizeof(long double)=8; this is not the PR test’s expected x87 encoding (mantissa= c000000000000000, exponent=7fff)
Hosted Windows job Pass; the selective CTest step was skipped
Linux and specialized checks Linux skipped; several macOS jobs were pending when checked
AddressSanitizer output
=================================================================
==2079376==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffe5e0c2078 at pc 0x7f082884814b bp 0x7ffe5e0c2030 sp 0x7ffe5e0c17e0
READ of size 10 at 0x7ffe5e0c2078 thread T0
    #0 0x7f082884814a in __interceptor_memcpy ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827
    #1 0x559816915685 in void StoreDefaultNaN<long double>(void*, unsigned long) /tmp/pr12532_ld80_probe.cpp:12
    #2 0x559816915312 in main /tmp/pr12532_ld80_probe.cpp:19
    #3 0x7f0828245249  (/lib/x86_64-linux-gnu/libc.so.6+0x27249)
    #4 0x7f0828245304 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x27304)
    #5 0x559816915130 in _start (/tmp/pr12532_ld80_probe+0x1130)

Address 0x7ffe5e0c2078 is located in stack of thread T0 at offset 40 in frame
    #0 0x559816915523 in void StoreDefaultNaN<long double>(void*, unsigned long) /tmp/pr12532_ld80_probe.cpp:7

  This frame has 1 object(s):
    [32, 40) 'value' (line 8) <== Memory access at offset 40 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
SUMMARY: AddressSanitizer: stack-buffer-overflow ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827 in __interceptor_memcpy
==2079376==ABORTING

Negative control

Assertion Change to code under test Did the check fail?
No read beyond the temporary and the guest receives the expected 80-bit NaN No mutant/negative control run; the ASan probe reproduced the current code’s failure Not applicable

Findings

Blocking

  • StoreDefaultNaN<long double>(target, 10) assumes the host temporary contains at least 10 bytes. On macOS ARM, the PR’s own debt entry notes that long double is double, so sizeof(value) is 8. The 10-byte memcpy reads two bytes beyond the local object, and copying the host binary64 NaN bytes does not produce the 80-bit guest representation asserted by the test. Encode the guest’s 80-bit quiet NaN using an explicit guest-format representation (or an equivalent host-independent conversion), and add/run a test with sizeof(long double) == 8 under AddressSanitizer.

Non-blocking

None identified in this check.

Sources

Not verified

I did not run the complete project test suite or execute this on a physical Apple Silicon host. The sanitizer probe models an 8-byte long double ABI and exercises the exact helper operation; hosted macOS formatting tests were not complete when checked.

To get this to a good state

Please make the NaN normalization safe when the host long double is 8 bytes and ensure it writes the guest’s 80-bit representation. Add a focused regression that runs with that ABI under AddressSanitizer. I will rerun the same probe and review the updated diff on the new head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5687641831 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12532#pullrequestreview-5687641831). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T18:23:14Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested On hosts where `long double` is 8 bytes, the new `%Lf` NaN normalization copies 10 bytes from an 8-byte temporary, which AddressSanitizer reports as a stack-buffer-overflow; it also does not encode the expected x87 80-bit quiet NaN. ### What I ran Reviewed head `92c6891d1cdec64888aac01dbabdcea24b97101e` against its declared base `e4da19aff06e25ae16b90f460dff22e8ac1eb786`; the cumulative PR merges cleanly onto current `main` at `f21ef7c99c5af648c5a41bddb18ce118b8e60c46`. Environment: Debian 12 x86_64, GCC 12.2. `git diff --check` passed. I also compiled the new helper behavior with GCC’s `-mlong-double-64` mode to model the 8-byte `long double` ABI noted for macOS ARM in this PR’s own documentation. ```sh git merge-tree $(git merge-base upstream/main pr/12532) upstream/main pr/12532 git diff --check upstream/main...pr/12532 g++ -std=c++20 -mlong-double-64 -fsanitize=address -g /tmp/pr12532_ld80_probe.cpp -o /tmp/pr12532_ld80_probe /tmp/pr12532_ld80_probe ``` The probe source applies the PR’s `StoreDefaultNaN<T>` implementation to a 16-byte guest buffer whose first eight bytes contain a negative binary64 NaN, with `StoreDefaultNaN<long double>(guest, 10)`. The address sanitizer reports the 10-byte read from the 8-byte local `value`. ### Results | Check | Result | |---|---| | Merge onto current `main` | Clean | | `git diff --check` | Pass; no output | | 8-byte `long double` ASan probe | Fails with stack-buffer-overflow at the `memcpy(target, &value, bytes)` operation | | Non-ASan probe output | `mantissa=7ff8000000000000 exponent=b2a0 sizeof(long double)=8`; this is not the PR test’s expected x87 encoding (`mantissa= c000000000000000`, `exponent=7fff`) | | Hosted Windows job | Pass; the selective CTest step was skipped | | Linux and specialized checks | Linux skipped; several macOS jobs were pending when checked | <details> <summary>AddressSanitizer output</summary> ```text ================================================================= ==2079376==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffe5e0c2078 at pc 0x7f082884814b bp 0x7ffe5e0c2030 sp 0x7ffe5e0c17e0 READ of size 10 at 0x7ffe5e0c2078 thread T0 #0 0x7f082884814a in __interceptor_memcpy ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827 #1 0x559816915685 in void StoreDefaultNaN<long double>(void*, unsigned long) /tmp/pr12532_ld80_probe.cpp:12 #2 0x559816915312 in main /tmp/pr12532_ld80_probe.cpp:19 #3 0x7f0828245249 (/lib/x86_64-linux-gnu/libc.so.6+0x27249) #4 0x7f0828245304 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x27304) #5 0x559816915130 in _start (/tmp/pr12532_ld80_probe+0x1130) Address 0x7ffe5e0c2078 is located in stack of thread T0 at offset 40 in frame #0 0x559816915523 in void StoreDefaultNaN<long double>(void*, unsigned long) /tmp/pr12532_ld80_probe.cpp:7 This frame has 1 object(s): [32, 40) 'value' (line 8) <== Memory access at offset 40 overflows this variable HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork SUMMARY: AddressSanitizer: stack-buffer-overflow ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827 in __interceptor_memcpy ==2079376==ABORTING ``` </details> ### Negative control | Assertion | Change to code under test | Did the check fail? | |---|---|---| | No read beyond the temporary and the guest receives the expected 80-bit NaN | No mutant/negative control run; the ASan probe reproduced the current code’s failure | Not applicable | ### Findings #### Blocking - `StoreDefaultNaN<long double>(target, 10)` assumes the host temporary contains at least 10 bytes. On macOS ARM, the PR’s own debt entry notes that `long double` is `double`, so `sizeof(value)` is 8. The 10-byte `memcpy` reads two bytes beyond the local object, and copying the host binary64 NaN bytes does not produce the 80-bit guest representation asserted by the test. Encode the guest’s 80-bit quiet NaN using an explicit guest-format representation (or an equivalent host-independent conversion), and add/run a test with `sizeof(long double) == 8` under AddressSanitizer. #### Non-blocking None identified in this check. ### Sources - [`StoreDefaultNaN` and its `%Lf` call at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/92c6891d1cdec64888aac01dbabdcea24b97101e/core/libs/prx/libc/src/Formatting.cpp#L164-L171) - [The caller that requests a 10-byte guest write](https://github.com/boykopovar/AnyPS5/blob/92c6891d1cdec64888aac01dbabdcea24b97101e/core/libs/prx/libc/src/Formatting.cpp#L373-L379) - [The PR’s documentation noting `long double` is `double` on macOS ARM](https://github.com/boykopovar/AnyPS5/blob/92c6891d1cdec64888aac01dbabdcea24b97101e/docs/dev/debt/libc.md#L103) - [The test assertion for the expected 80-bit NaN bytes](https://github.com/boykopovar/AnyPS5/blob/92c6891d1cdec64888aac01dbabdcea24b97101e/core/libs/tests/Formatting.cpp#L372-L381) ### Not verified I did not run the complete project test suite or execute this on a physical Apple Silicon host. The sanitizer probe models an 8-byte `long double` ABI and exercises the exact helper operation; hosted macOS formatting tests were not complete when checked. ### To get this to a good state Please make the NaN normalization safe when the host `long double` is 8 bytes and ensure it writes the guest’s 80-bit representation. Add a focused regression that runs with that ABI under AddressSanitizer. I will rerun the same probe and review the updated diff on the new head.
rooklz commented 2026-10-28 18:30:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T18:30:08Z.

Review: COMMENTED


Reviewed the top commit 92c6891d1c only, as the body asks. #12530 and #12507 are below it.

Linux build (Ubuntu 24.04 amd64 container, GCC 13, Release, BUILD_TESTING=ON, head 92c6891d1c)

  • guest_formatting, guest_stdio_variadic, guest_scan_count: all 3 pass.
  • RED check: I put the parent's (3eca2be067) Formatting.cpp under the head tests, and guest_formatting fails. The new checks catch the old behaviour.

What glibc stores, and what the helper does with it (I copied StoreDefaultNaN into a standalone program, same container)

input host %f / %lf / %Lf after StoreDefaultNaN
-nan, -NaN, -nan(0x5) ffc00000 / fff8000000000000 / ffff:c000000000000000 7fc00000 / 7ff8000000000000 / 7fff:c000000000000000
nan, NAN, +nan, nan(), nan(1), nan(0x123) already the default NaN unchanged
-inf, -0, 0x1p3 sign kept unchanged (the NaN check gates correctly)

For %Lf, bytes 10..15 stay as they were. Only the 10 value bytes are written, matching what the host stores.

Mutants against the head tests

mutant result
drop the L branch killed
drop the l branch (falls to float) killed
%Lf writes sizeof(long double) (16) instead of 10 survives
fix only sign-bit NaNs (!isnan || !signbit) survives. Equivalent on glibc, which never yields a positive non-default NaN from text (payloads are dropped, see the table)
!suppress → target != nullptr survives. Equivalent, since a null target already returns EOF

The surviving 16-byte mutant would overwrite the 6 bytes after a guest's 10-byte long double. The extended buffer in the test is uninitialised and only bytes 0..9 are checked, so nothing catches it. This test-only change kills it (head passes, mutant and parent fail):

             unsigned char extended[16];
+            std::memset(extended, 0x55, sizeof(extended));
             Require(ScanListS(input, "%f", &single) == 1 && std::bit_cast<std::uint32_t>(single) == 0x7fc00000u);
@@
             Require(mantissa == 0xc000000000000000ull && exponent == 0x7fff);
+            Require(std::all_of(extended + 10, extended + 16, [](unsigned char byte) { return byte == 0x55; }));
         }

(<algorithm> is already included.)

Other checks

  • memcpy(target, &value, 10) from a host long double is safe, because the libc target already static-asserts an x87 80-bit long double (Math.cpp:140), and macOS builds of the libs force x86_64.
  • check_conventions --base upstream/main passes, and git merge-tree against upstream/main f21ef7c99c is clean. pr_overlap shows file-level sharing only on docs/dev/debt/libc.md and with your #12397, and a pairwise merge-tree with #12397 is clean.

No defects in the fix. The one suggestion is the padding check above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5687732735 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12532#pullrequestreview-5687732735). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T18:30:08Z. **Review:** COMMENTED --- Reviewed the top commit `92c6891d1c` only, as the body asks. #12530 and #12507 are below it. **Linux build** (Ubuntu 24.04 amd64 container, GCC 13, Release, `BUILD_TESTING=ON`, head `92c6891d1c`) - `guest_formatting`, `guest_stdio_variadic`, `guest_scan_count`: all 3 pass. - RED check: I put the parent's (`3eca2be067`) `Formatting.cpp` under the head tests, and `guest_formatting` fails. The new checks catch the old behaviour. **What glibc stores, and what the helper does with it** (I copied `StoreDefaultNaN` into a standalone program, same container) | input | host `%f` / `%lf` / `%Lf` | after `StoreDefaultNaN` | |---|---|---| | `-nan`, `-NaN`, `-nan(0x5)` | `ffc00000` / `fff8000000000000` / `ffff:c000000000000000` | `7fc00000` / `7ff8000000000000` / `7fff:c000000000000000` | | `nan`, `NAN`, `+nan`, `nan()`, `nan(1)`, `nan(0x123)` | already the default NaN | unchanged | | `-inf`, `-0`, `0x1p3` | sign kept | unchanged (the NaN check gates correctly) | For `%Lf`, bytes 10..15 stay as they were. Only the 10 value bytes are written, matching what the host stores. **Mutants** against the head tests | mutant | result | |---|---| | drop the `L` branch | killed | | drop the `l` branch (falls to float) | killed | | `%Lf` writes `sizeof(long double)` (16) instead of 10 | **survives** | | fix only sign-bit NaNs (`!isnan \|\| !signbit`) | survives. Equivalent on glibc, which never yields a positive non-default NaN from text (payloads are dropped, see the table) | | `!suppress` → `target != nullptr` | survives. Equivalent, since a null target already returns `EOF` | The surviving 16-byte mutant would overwrite the 6 bytes after a guest's 10-byte `long double`. The extended buffer in the test is uninitialised and only bytes 0..9 are checked, so nothing catches it. This test-only change kills it (head passes, mutant and parent fail): ```diff unsigned char extended[16]; + std::memset(extended, 0x55, sizeof(extended)); Require(ScanListS(input, "%f", &single) == 1 && std::bit_cast<std::uint32_t>(single) == 0x7fc00000u); @@ Require(mantissa == 0xc000000000000000ull && exponent == 0x7fff); + Require(std::all_of(extended + 10, extended + 16, [](unsigned char byte) { return byte == 0x55; })); } ``` (`<algorithm>` is already included.) **Other checks** - `memcpy(target, &value, 10)` from a host `long double` is safe, because the libc target already static-asserts an x87 80-bit `long double` (`Math.cpp:140`), and macOS builds of the libs force x86_64. - `check_conventions --base upstream/main` passes, and `git merge-tree` against `upstream/main` f21ef7c99c is clean. `pr_overlap` shows file-level sharing only on `docs/dev/debt/libc.md` and with your #12397, and a pairwise merge-tree with #12397 is clean. No defects in the fix. The one suggestion is the padding check above.
MarcZX10 commented 2026-10-31 11:19:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T11:19:46Z.

Review: APPROVED


Re-checked — new head, looks good

At 6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c, the %Lf NaN helper reads only the host value width, writes the ten-byte guest x87 NaN explicitly, and preserves trailing padding.

What I ran

Reviewed head 6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c against base 157b02c4f1803e9d2cd757677b9a805961203fc4; the dependency stack preview was clean against current main f5b82ff45302cd6748a58868611a3dbe89211cdd. Environment: Linux x86_64, Python 3.11.2. No local C++ build or test was run.

git diff --check 157b02c4f1803e9d2cd757677b9a805961203fc4...6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c
python3 tools/check_conventions.py --base 157b02c4f1803e9d2cd757677b9a805961203fc4 --head 6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c --title 'fix(libc): store a scanned NaN as the default quiet NaN in sscanf_s' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-12532-6d16add5fb12/pr-body.md
python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 12532

Results

Check Result
Diff and conventions Passed
Dependency integration #12507 → #12530 → #12532 all preview cleanly against current main; declared dependencies are included in the head
Hosted checks Windows, Linux relinker, macOS relinker, ASan, register-context, and write-watch jobs passed; product Linux job was skipped
Focused test evidence PR participant reports 3/3 passes for the three focused Linux groups and 5/5 Windows guest_formatting runs on the current head
Precheck helper availability Its optional ABI and test-quality helper copies were absent from the isolated PR checkout; no public exports change in this PR
Unedited local convention output
All conventions checks passed.

Negative control

Assertion Change to code under test Outcome
Ten-byte guest NaN write preserves bytes 10–15 PR participant reports changing production output to 16 bytes while retaining the test Test failed as expected; the reported restored source passed
Helper avoids an 8-byte-host overread PR participant reports an ASan+UBSan helper harness using GCC -mlong-double-64 The parent reproduced the overread; the corrected helper passed the reported control. I did not run this harness locally.

Sources

Not verified

I did not reproduce the native title comparison, Windows or Linux tests, or sanitizer harness. The PR explicitly does not establish a full libc port for hosts whose long double is eight bytes; that broader platform behavior remains outside this change. Console behavior beyond the cited title measurements is not verified.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5722504984 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12532#pullrequestreview-5722504984). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T11:19:46Z. **Review:** APPROVED --- # Re-checked — new head, looks good At `6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c`, the `%Lf` NaN helper reads only the host value width, writes the ten-byte guest x87 NaN explicitly, and preserves trailing padding. ## What I ran Reviewed head `6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c` against base `157b02c4f1803e9d2cd757677b9a805961203fc4`; the dependency stack preview was clean against current `main` `f5b82ff45302cd6748a58868611a3dbe89211cdd`. Environment: Linux x86_64, Python 3.11.2. No local C++ build or test was run. ```sh git diff --check 157b02c4f1803e9d2cd757677b9a805961203fc4...6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c python3 tools/check_conventions.py --base 157b02c4f1803e9d2cd757677b9a805961203fc4 --head 6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c --title 'fix(libc): store a scanned NaN as the default quiet NaN in sscanf_s' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-12532-6d16add5fb12/pr-body.md python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 12532 ``` ## Results | Check | Result | |---|---| | Diff and conventions | Passed | | Dependency integration | #12507 → #12530 → #12532 all preview cleanly against current `main`; declared dependencies are included in the head | | Hosted checks | Windows, Linux relinker, macOS relinker, ASan, register-context, and write-watch jobs passed; product Linux job was skipped | | Focused test evidence | PR participant reports 3/3 passes for the three focused Linux groups and 5/5 Windows `guest_formatting` runs on the current head | | Precheck helper availability | Its optional ABI and test-quality helper copies were absent from the isolated PR checkout; no public exports change in this PR | <details><summary>Unedited local convention output</summary> ```text All conventions checks passed. ``` </details> ## Negative control | Assertion | Change to code under test | Outcome | |---|---|---| | Ten-byte guest NaN write preserves bytes 10–15 | PR participant reports changing production output to 16 bytes while retaining the test | Test failed as expected; the reported restored source passed | | Helper avoids an 8-byte-host overread | PR participant reports an ASan+UBSan helper harness using GCC `-mlong-double-64` | The parent reproduced the overread; the corrected helper passed the reported control. I did not run this harness locally. | ## Sources - [NaN normalization helper at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c/core/libs/prx/libc/src/Formatting.cpp#L175-L187) bounds the host read, detects NaN, and writes the guest x87 representation for the ten-byte destination. - [Regression assertions at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/6d16add5fb12bcc42bb50e987a07b9ab90ee4f4c/core/libs/tests/Formatting.cpp#L391-L404) check float, double, x87 bytes, and untouched padding. - [Linux qualification and bounded-helper report](https://github.com/boykopovar/AnyPS5/pull/12532#issuecomment-6498364264); [Windows test report on the current head](https://github.com/boykopovar/AnyPS5/pull/12532#issuecomment-6540088861). ## Not verified I did not reproduce the native title comparison, Windows or Linux tests, or sanitizer harness. The PR explicitly does not establish a full libc port for hosts whose `long double` is eight bytes; that broader platform behavior remains outside this change. Console behavior beyond the cited title measurements is not verified.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12532
No description provided.