fix(libc): convert scanf integers as FreeBSD does on Windows #4815

Closed
EagleBearerYS wants to merge 0 commits from upstream-pr/4815 into main
EagleBearerYS commented 2026-10-12 22:43:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-12T22:43:48Z.


What

On Windows the scanf family (sscanf, vsscanf, fscanf and their callers) translates the guest's length modifiers and hands the format to the host's scanner. Two integer cases differ from FreeBSD 11.4's vfscanf.

  1. A %d, %i, %o, %u, %x or %X without a length modifier, or with h or hh, went to the host unchanged. The host converts it with its 32-bit long, so an out-of-range value was clamped and errno set to ERANGE. FreeBSD converts every integer with strtoimax or strtoumax and stores the result truncated to the destination type (vfscanf.c L741-L771).
  2. q, FreeBSD's quad length modifier, was not recognized, so %qd, %qu and %qx threw NotImplemented_nid_no_patch ("vsscanf format conversion"). FreeBSD treats q like ll (vfscanf.c L529-L531).

Measured with main's code:

Input Format Windows Linux, and FreeBSD
"2147483648" %d 2147483647, errno 34 -2147483648
"3000000000" %i 2147483647, errno 34 -1294967296
"4294967296" %u 4294967295, errno 34 0
"100000000" %x 4294967295, errno 34 0
"3000000000" %qd throws 3000000000

A colour or mask written as 0xFFFFFFFF and read with %i into an int therefore came back as 0x7FFFFFFF.

ScanWindowsArguments_nid_no_patch (WindowsScanning.hpp) now does two things:

  • It translates q as it does l and ll.
  • It scans such a 32-bit-or-narrower conversion as ll into a temporary. A %n after it shows whether the conversion took place. The value is then stored truncated to the destination's int, short or char, as FreeBSD's assignment does.

A value beyond the 64-bit range still sets ERANGE, as strtoimax does there. Linux hands the format to glibc, which already behaves this way. No title is claimed.

Tested

Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release, -DBUILD_TESTING=ON, on main at 43e3a70f8:

  • New CheckIntegerScanWidths in guest_formatting checks:
    • truncation for %d, %i ("0xFFFFFFFF" gives -1), %u, %x, %o, %hd ("5000000000" gives -3584) and %hhd ("300" gives 44), with errno left alone;
    • ERANGE for "99999999999999999999" with %d (-1);
    • %qd and %qx;
    • that %n still counts after the new conversions, and that a failed conversion leaves its destination unchanged.
  • With main's WindowsScanning.hpp the test fails at its first check ("2147483648" with %d gives 2147483647 and errno 34). With the change it passes, 20 runs in a row.
  • All 128 tests registered in core/libs/prx/libc/CMakeLists.txt and core/libs/prx/libkernel/CMakeLists.txt that build on Windows pass.
  • Linux: WSL2 (Ubuntu 24.04, GCC 13.3.0, Release with CI's configure flags), same commit. guest_formatting passes 20 runs in a row; the code change does not apply there, and glibc already gives these results.
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: macOS, 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
  • 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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4835859413 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4815). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-12T22:43:48Z. --- ### What On Windows the scanf family (`sscanf`, `vsscanf`, `fscanf` and their callers) translates the guest's length modifiers and hands the format to the host's scanner. Two integer cases differ from FreeBSD 11.4's `vfscanf`. 1. A `%d`, `%i`, `%o`, `%u`, `%x` or `%X` without a length modifier, or with `h` or `hh`, went to the host unchanged. The host converts it with its 32-bit `long`, so an out-of-range value was clamped and `errno` set to `ERANGE`. FreeBSD converts every integer with `strtoimax` or `strtoumax` and stores the result truncated to the destination type ([`vfscanf.c` L741-L771](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/vfscanf.c#L741-L771)). 2. `q`, FreeBSD's quad length modifier, was not recognized, so `%qd`, `%qu` and `%qx` threw `NotImplemented_nid_no_patch` ("vsscanf format conversion"). FreeBSD treats `q` like `ll` ([`vfscanf.c` L529-L531](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/vfscanf.c#L529-L531)). Measured with `main`'s code: | Input | Format | Windows | Linux, and FreeBSD | |---|---|---|---| | `"2147483648"` | `%d` | 2147483647, `errno` 34 | -2147483648 | | `"3000000000"` | `%i` | 2147483647, `errno` 34 | -1294967296 | | `"4294967296"` | `%u` | 4294967295, `errno` 34 | 0 | | `"100000000"` | `%x` | 4294967295, `errno` 34 | 0 | | `"3000000000"` | `%qd` | throws | 3000000000 | A colour or mask written as `0xFFFFFFFF` and read with `%i` into an `int` therefore came back as `0x7FFFFFFF`. `ScanWindowsArguments_nid_no_patch` (`WindowsScanning.hpp`) now does two things: - It translates `q` as it does `l` and `ll`. - It scans such a 32-bit-or-narrower conversion as `ll` into a temporary. A `%n` after it shows whether the conversion took place. The value is then stored truncated to the destination's `int`, `short` or `char`, as FreeBSD's assignment does. A value beyond the 64-bit range still sets `ERANGE`, as `strtoimax` does there. Linux hands the format to glibc, which already behaves this way. No title is claimed. ### Tested Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release, `-DBUILD_TESTING=ON`, on `main` at 43e3a70f8: - New `CheckIntegerScanWidths` in `guest_formatting` checks: - truncation for `%d`, `%i` (`"0xFFFFFFFF"` gives -1), `%u`, `%x`, `%o`, `%hd` (`"5000000000"` gives -3584) and `%hhd` (`"300"` gives 44), with `errno` left alone; - `ERANGE` for `"99999999999999999999"` with `%d` (-1); - `%qd` and `%qx`; - that `%n` still counts after the new conversions, and that a failed conversion leaves its destination unchanged. - With `main`'s `WindowsScanning.hpp` the test fails at its first check (`"2147483648"` with `%d` gives 2147483647 and `errno` 34). With the change it passes, 20 runs in a row. - All 128 tests registered in `core/libs/prx/libc/CMakeLists.txt` and `core/libs/prx/libkernel/CMakeLists.txt` that build on Windows pass. - Linux: WSL2 (Ubuntu 24.04, GCC 13.3.0, Release with CI's configure flags), same commit. `guest_formatting` passes 20 runs in a row; the code change does not apply there, and glibc already gives these results. - `tools/check_conventions.py --base origin/main` passes. - Not tested: macOS, any title. ### 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: none - [x] Unblocks: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-14 02:37:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:37:49Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159644714 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4815#issuecomment-6159644714). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:37:49Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!4815
No description provided.