fix(libc): end a wide scanf at a positional $ as FreeBSD #11726

Merged
cocoa55 merged 13 commits from upstream-pr/11726 into main 2026-11-14 03:39:50 +00:00
cocoa55 commented 2026-10-27 05:57:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cocoa55 on 2026-10-27T05:57:18Z.


Title: fix(libc): end a wide scanf at a positional $ as FreeBSD does

What

A scanf-family format that has a 16-bit wide conversion (%ls, %lc, %l[, %S, %C) and an n$ position used to throw NotImplemented ("sscanf positional arguments with a 16-bit wide conversion").

FreeBSD's vfscanf, which the console's libc is based on, has no n$ support. It reads n as a field width, and the $ is then an unknown conversion character, so the scan stops there as a matching failure and returns what it assigned so far. The in-process scanner (ScanSource) already does exactly that for any unknown conversion character, and main already relies on it for narrow %c formats (sscanf("a", "%2$2c%1$n", ...) == 0). So this PR only removes the guard:

  • RequireNoPositionalWideScan is removed, so wide formats with a $ now take the same path as the narrow %c formats.
  • ScansWideInProcess is removed. Its callers now test HasGuestWideScan directly.

The debt entry now says that an n$ with a wide field fails at the $ as in FreeBSD, and that this is unchecked on a console.

This replaces the earlier version of this PR, which added glibc-style n$ support. Main has since pinned the FreeBSD behaviour in a test, so that version no longer fits.

Tested

Run by Claude (AI) on Linux (Ubuntu, GCC 13.3), Release build with -DBUILD_TESTING=ON, on top of current main:

  • guest_formatting_tests: sscanf("a b", "%2$ls") returns 0 and leaves the buffer untouched, and sscanf("5 ab", "%d %1$ls") returns 1 with only the number assigned. These replace the old "%2$ls throws" case.
  • guest_wide_stream_scan_tests: fscanf with %d %2$ls returns 1, and the next fgetc still reads the a after the $.
  • Negative control: with the src changes reverted, guest_formatting_tests aborts with the old "not implemented" error.
  • guest_scan_count, guest_wide_formatting, guest_stdio_variadic pass. tools/check_conventions.py passes, and tools/progress.py shows no change in libc stubs.
  • Windows not built. Windows wide formats go through the same ScanSource, so the change applies there too.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5029341976 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11726). Originally posted by [cocoa55](https://github.com/cocoa55) on 2026-10-27T05:57:18Z. --- **Title:** fix(libc): end a wide scanf at a positional $ as FreeBSD does ### What A scanf-family format that has a 16-bit wide conversion (`%ls`, `%lc`, `%l[`, `%S`, `%C`) and an `n$` position used to throw `NotImplemented` ("sscanf positional arguments with a 16-bit wide conversion"). FreeBSD's `vfscanf`, which the console's libc is based on, has no `n$` support. It reads `n` as a field width, and the `$` is then an unknown conversion character, so the scan stops there as a matching failure and returns what it assigned so far. The in-process scanner (`ScanSource`) already does exactly that for any unknown conversion character, and main already relies on it for narrow `%c` formats (`sscanf("a", "%2$2c%1$n", ...) == 0`). So this PR only removes the guard: - `RequireNoPositionalWideScan` is removed, so wide formats with a `$` now take the same path as the narrow `%c` formats. - `ScansWideInProcess` is removed. Its callers now test `HasGuestWideScan` directly. The debt entry now says that an `n$` with a wide field fails at the `$` as in FreeBSD, and that this is unchecked on a console. This replaces the earlier version of this PR, which added glibc-style `n$` support. Main has since pinned the FreeBSD behaviour in a test, so that version no longer fits. ### Tested Run by Claude (AI) on Linux (Ubuntu, GCC 13.3), Release build with `-DBUILD_TESTING=ON`, on top of current main: - `guest_formatting_tests`: `sscanf("a b", "%2$ls")` returns 0 and leaves the buffer untouched, and `sscanf("5 ab", "%d %1$ls")` returns 1 with only the number assigned. These replace the old "`%2$ls` throws" case. - `guest_wide_stream_scan_tests`: `fscanf` with `%d %2$ls` returns 1, and the next `fgetc` still reads the `a` after the `$`. - Negative control: with the `src` changes reverted, `guest_formatting_tests` aborts with the old "not implemented" error. - `guest_scan_count`, `guest_wide_formatting`, `guest_stdio_variadic` pass. `tools/check_conventions.py` passes, and `tools/progress.py` shows no change in libc stubs. - Windows not built. Windows wide formats go through the same `ScanSource`, so the change applies there too. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 06:17:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T06:17:21Z.

Review: COMMENTED


One point in the argument-position description needs tightening. The code and PR text define what happens when a format mixes numbered assignments (%n$) with unnumbered assignments, and say that this matches glibc. POSIX says those forms cannot be mixed in one format (except %% and assignment-suppressed %* conversions); otherwise the result is undefined: https://pubs.opengroup.org/onlinepubs/000095399/functions/fscanf.html. The added tests use numbered assignments throughout (the unnumbered %*ls case is suppressed), so they do not establish the mixed behavior claimed in the description. Please describe mixed assignment formats as unverified/unsupported, or add a test and document the chosen extension without attributing it to glibc. This is not a correctness blocker for formats that use numbered arguments consistently.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5662986889 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11726#pullrequestreview-5662986889). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T06:17:21Z. **Review:** COMMENTED --- One point in the argument-position description needs tightening. The code and PR text define what happens when a format mixes numbered assignments (`%n$`) with unnumbered assignments, and say that this matches glibc. POSIX says those forms cannot be mixed in one format (except `%%` and assignment-suppressed `%*` conversions); otherwise the result is undefined: https://pubs.opengroup.org/onlinepubs/000095399/functions/fscanf.html. The added tests use numbered assignments throughout (the unnumbered `%*ls` case is suppressed), so they do not establish the mixed behavior claimed in the description. Please describe mixed assignment formats as unverified/unsupported, or add a test and document the chosen extension without attributing it to glibc. This is not a correctness blocker for formats that use numbered arguments consistently.
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!11726
No description provided.