feat(libc): scan 16-bit wide conversions in the sscanf family on Linux #7525

Merged
AlexOfTheForest merged 1 commit from upstream-pr/7525 into main 2026-10-21 11:09:55 +00:00
AlexOfTheForest commented 2026-10-18 12:06:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-18T12:06:17Z.


What

On Linux, sscanf, vsscanf, sscanf_s and vsscanf_s threw for the 16-bit wide conversions (%ls, %lc, %l[, %S, %C), because glibc would store a 32-bit wchar_t per character into the guest's 16-bit buffer. ScanGuest, the per-directive scanner the _s forms already use, now scans those fields itself:

  • The input is decoded as UTF-8, the inverse of the narrow printf family's documented UTF-8 output. Each character is stored as one 16-bit unit; the width and the _s size count characters.
  • %ls skips leading white space and stops at the next. %l[ matches an ASCII scanset with ranges, negation and a leading ]. %lc reads the field width (default 1) without a terminator; a field that meets the end of the input early keeps what it read and counts as converted, as FreeBSD's convert_wchar does.
  • _s sizing mirrors the narrow path: a too-small array gets a null first element and the conversion fails.
  • sscanf and vsscanf send a format with a wide conversion through ScanGuest; any other format keeps the host path.

UTF-8 decoding and scanset matching live in the new ScanWideField.cpp. It throws for input that is not UTF-8, a character outside the BMP, a scanset member outside ASCII, and positional arguments next to a wide conversion. Wide conversions in fscanf still throw, since it reads a stream, not a string. The libc debt entry records all of this, plus the fact that FreeBSD decodes with the locale's mbrtowc and that the console's locale encoding is unverified.

Progress report: vsscanf_nid_postfix moves from stub to implemented (+1). Its Linux path now handles these formats instead of throwing; the remaining unsupported cases throw from ScanWideField.cpp.

Tested

Linux x86_64:

  • All 79 libc tests were built and run, and all pass.
  • The guest_formatting cases replacing the old "rejects" checks cover: %ls/%lc/%S/%C, %3lc without a terminator, scansets (range, negation, leading ]), Czech UTF-8 text with widths, suppression, mixing with narrow and %d/%n, EOF and matching failures, vsscanf through a va_list, the _s sizing cases, and each remaining throw.
  • With the change reverted, guest_formatting fails.
  • tools/check_conventions.py --base upstream/main passes.
  • Not run on Windows, where these conversions keep going to the host, as before.

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:4908947249 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7525). Originally posted by [AlexOfTheForest](https://github.com/AlexOfTheForest) on 2026-10-18T12:06:17Z. --- ### What On Linux, `sscanf`, `vsscanf`, `sscanf_s` and `vsscanf_s` threw for the 16-bit wide conversions (`%ls`, `%lc`, `%l[`, `%S`, `%C`), because glibc would store a 32-bit `wchar_t` per character into the guest's 16-bit buffer. `ScanGuest`, the per-directive scanner the `_s` forms already use, now scans those fields itself: - The input is decoded as UTF-8, the inverse of the narrow printf family's documented UTF-8 output. Each character is stored as one 16-bit unit; the width and the `_s` size count characters. - `%ls` skips leading white space and stops at the next. `%l[` matches an ASCII scanset with ranges, negation and a leading `]`. `%lc` reads the field width (default 1) without a terminator; a field that meets the end of the input early keeps what it read and counts as converted, as FreeBSD's `convert_wchar` does. - `_s` sizing mirrors the narrow path: a too-small array gets a null first element and the conversion fails. - `sscanf` and `vsscanf` send a format with a wide conversion through `ScanGuest`; any other format keeps the host path. UTF-8 decoding and scanset matching live in the new `ScanWideField.cpp`. It throws for input that is not UTF-8, a character outside the BMP, a scanset member outside ASCII, and positional arguments next to a wide conversion. Wide conversions in `fscanf` still throw, since it reads a stream, not a string. The libc debt entry records all of this, plus the fact that FreeBSD decodes with the locale's `mbrtowc` and that the console's locale encoding is unverified. Progress report: `vsscanf_nid_postfix` moves from stub to implemented (+1). Its Linux path now handles these formats instead of throwing; the remaining unsupported cases throw from `ScanWideField.cpp`. ### Tested Linux x86_64: - All 79 libc tests were built and run, and all pass. - The `guest_formatting` cases replacing the old "rejects" checks cover: `%ls`/`%lc`/`%S`/`%C`, `%3lc` without a terminator, scansets (range, negation, leading `]`), Czech UTF-8 text with widths, suppression, mixing with narrow and `%d`/`%n`, `EOF` and matching failures, `vsscanf` through a `va_list`, the `_s` sizing cases, and each remaining throw. - With the change reverted, `guest_formatting` fails. - `tools/check_conventions.py --base upstream/main` passes. - Not run on Windows, where these conversions keep going to the host, as before. ### 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](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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-18 12:07:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T12:07:01Z.


Progress report (9127ea3 → 675ff30)

📈 System libraries: 79.85% (+0.02%, +1 implemented functions)

✅ 1 implemented
Library Name
libc vsscanf_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249718833 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7525#issuecomment-6249718833). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T12:07:01Z. --- <!-- progress-report --> ### Progress report (9127ea3 → 675ff30) 📈 **System libraries**: 79.85% (+0.02%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `vsscanf_nid_postfix` | </details>
MarcZX10 commented 2026-10-18 12:09:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T12:09:20Z.

Code review location: core/libs/prx/libc/src/ScanWideField.cpp; line 65

@@ -0,0 +1,68 @@
+#include "prx/libc/include/ScanWideField.hpp"
+#include "prx/libc/include/General.hpp"
+
+#include <cstring>
+
+namespace {
+
+std::size_t DecodeUtf8(const char* text, char32_t* point) {
+    const auto lead = static_cast<unsigned char>(text[0]);
+    if (lead < 0x80) {
+        *point = lead;
+        return 1;
+    }
+    std::size_t size = 0;
+    char32_t value = 0;
+    if ((lead & 0xE0) == 0xC0) { size = 2; value = lead & 0x1F; }
+    else if ((lead & 0xF0) == 0xE0) { size = 3; value = lead & 0x0F; }
+    else if ((lead & 0xF8) == 0xF0) { size = 4; value = lead & 0x07; }
+    else return 0;
+    for (std::size_t index = 1; index < size; ++index) {
+        const auto next = static_cast<unsigned char>(text[index]);
+        if ((next & 0xC0) != 0x80) return 0;
+        value = (value << 6) | (next & 0x3F);
+    }
+    constexpr char32_t Smallest[] = {0, 0, 0x80, 0x800, 0x10000};
+    if (value < Smallest[size] || value > 0x10FFFF || (value >= 0xD800 && value <= 0xDFFF)) return 0;
+    *point = value;
+    return size;
+}
+
+}
+
+namespace LibcDetail {
+
+std::size_t ReadWideScanCharacter(const char* text, char32_t* point) {
+    const std::size_t size = DecodeUtf8(text, point);
+    if (size == 0) NotImplemented_nid_no_patch("scanf 16-bit wide conversion of input that is not UTF-8");
+    if (*point > 0xFFFF) NotImplemented_nid_no_patch("scanf 16-bit wide conversion of a character outside the BMP");
+    return size;
+}
+
+bool InWideScanset(const std::string& specifier, char32_t point) {
+    std::size_t index = 1;
+    const bool negated = specifier[index] == '^';
+    if (negated) ++index;
+    const std::size_t end = specifier.size() - 1;
+    bool member = false;
+    for (std::size_t first = index; index < end; ++index) {
+        const auto value = static_cast<unsigned char>(specifier[index]);
+        if (value >= 0x80) NotImplemented_nid_no_patch("scanf 16-bit wide scanset with a character outside ASCII");
+        if (specifier[index] == '-' && index != first && index + 1 < end) {
+            const auto low = static_cast<unsigned char>(specifier[index - 1]);
+            const auto high = static_cast<unsigned char>(specifier[index + 1]);
+            if (high >= 0x80) NotImplemented_nid_no_patch("scanf 16-bit wide scanset with a character outside ASCII");
+            if (point >= low && point <= high) member = true;
+            ++index;
+            continue;
+        }
+        if (point == value) member = true;
+    }
+    return member != negated;
+}
+
+void RequireNoPositionalWideScan(const char* format) {
+    if (std::strchr(format, '$') != nullptr) NotImplemented_nid_no_patch("sscanf positional arguments with a 16-bit wide conversion on Linux");

This rejects any $ anywhere in the format, including a literal dollar sign. For example, scanning input "$word" with format "$%ls" reaches this guard and throws, even though the $ is not part of a positional conversion. Could this detect $ only inside conversion directives (or otherwise distinguish literal text) before rejecting positional wide scans?

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4306955547 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7525#discussion_r4306955547). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T12:09:20Z. **Code review location:** `core/libs/prx/libc/src/ScanWideField.cpp`; line 65 ~~~~diff @@ -0,0 +1,68 @@ +#include "prx/libc/include/ScanWideField.hpp" +#include "prx/libc/include/General.hpp" + +#include <cstring> + +namespace { + +std::size_t DecodeUtf8(const char* text, char32_t* point) { + const auto lead = static_cast<unsigned char>(text[0]); + if (lead < 0x80) { + *point = lead; + return 1; + } + std::size_t size = 0; + char32_t value = 0; + if ((lead & 0xE0) == 0xC0) { size = 2; value = lead & 0x1F; } + else if ((lead & 0xF0) == 0xE0) { size = 3; value = lead & 0x0F; } + else if ((lead & 0xF8) == 0xF0) { size = 4; value = lead & 0x07; } + else return 0; + for (std::size_t index = 1; index < size; ++index) { + const auto next = static_cast<unsigned char>(text[index]); + if ((next & 0xC0) != 0x80) return 0; + value = (value << 6) | (next & 0x3F); + } + constexpr char32_t Smallest[] = {0, 0, 0x80, 0x800, 0x10000}; + if (value < Smallest[size] || value > 0x10FFFF || (value >= 0xD800 && value <= 0xDFFF)) return 0; + *point = value; + return size; +} + +} + +namespace LibcDetail { + +std::size_t ReadWideScanCharacter(const char* text, char32_t* point) { + const std::size_t size = DecodeUtf8(text, point); + if (size == 0) NotImplemented_nid_no_patch("scanf 16-bit wide conversion of input that is not UTF-8"); + if (*point > 0xFFFF) NotImplemented_nid_no_patch("scanf 16-bit wide conversion of a character outside the BMP"); + return size; +} + +bool InWideScanset(const std::string& specifier, char32_t point) { + std::size_t index = 1; + const bool negated = specifier[index] == '^'; + if (negated) ++index; + const std::size_t end = specifier.size() - 1; + bool member = false; + for (std::size_t first = index; index < end; ++index) { + const auto value = static_cast<unsigned char>(specifier[index]); + if (value >= 0x80) NotImplemented_nid_no_patch("scanf 16-bit wide scanset with a character outside ASCII"); + if (specifier[index] == '-' && index != first && index + 1 < end) { + const auto low = static_cast<unsigned char>(specifier[index - 1]); + const auto high = static_cast<unsigned char>(specifier[index + 1]); + if (high >= 0x80) NotImplemented_nid_no_patch("scanf 16-bit wide scanset with a character outside ASCII"); + if (point >= low && point <= high) member = true; + ++index; + continue; + } + if (point == value) member = true; + } + return member != negated; +} + +void RequireNoPositionalWideScan(const char* format) { + if (std::strchr(format, '$') != nullptr) NotImplemented_nid_no_patch("sscanf positional arguments with a 16-bit wide conversion on Linux"); ~~~~ --- This rejects any `$` anywhere in the format, including a literal dollar sign. For example, scanning input `"$word"` with format `"$%ls"` reaches this guard and throws, even though the `$` is not part of a positional conversion. Could this detect `$` only inside conversion directives (or otherwise distinguish literal text) before rejecting positional wide scans?
github-actions[bot] commented 2026-10-18 12:36:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T12:36:35Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 5303 docs/dev/debt/libc.md L114-115
PR 7386 core/libs/prx/libc/src/Formatting.cpp L11, L47-48, L54, L62-63, L157-161, L390, L409, L412, L431, L524
core/libs/tests/Formatting.cpp L284-294
docs/dev/debt/libc.md L115

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 3965 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 4627 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/Formatting.cpp
PR 5021 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 6044 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/Formatting.cpp
PR 6469 docs/dev/debt/libc.md
PR 6819 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 6879 docs/dev/debt/libc.md
PR 7081 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/Formatting.cpp
PR 7447 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7648 docs/dev/debt/libc.md
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8150 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8520 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8546 docs/dev/debt/libc.md
PR 8550 core/libs/prx/libc/src/Formatting.cpp
docs/dev/debt/libc.md
PR 8658 core/libs/prx/libc/CMakeLists.txt
core/libs/tests/Formatting.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249992828 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7525#issuecomment-6249992828). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T12:36:35Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 5303](https://www.github.com/boykopovar/AnyPS5/pull/5303) | `docs/dev/debt/libc.md` [L114-115](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/docs/dev/debt/libc.md#L114-L115) | | | | [PR 7386](https://www.github.com/boykopovar/AnyPS5/pull/7386) | `core/libs/prx/libc/src/Formatting.cpp` [L11](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L11), [L47-48](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L47-L48), [L54](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L54), [L62-63](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L62-L63), [L157-161](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L157-L161), [L390](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L390), [L409](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L409), [L412](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L412), [L431](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L431), [L524](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/prx/libc/src/Formatting.cpp#L524)<br>`core/libs/tests/Formatting.cpp` [L284-294](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/core/libs/tests/Formatting.cpp#L284-L294)<br>`docs/dev/debt/libc.md` [L115](https://github.com/boykopovar/AnyPS5/blob/2abfb1beb4dde0b8e6117d0998c6ac2f4f2d8666/docs/dev/debt/libc.md#L115) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 3965](https://www.github.com/boykopovar/AnyPS5/pull/3965) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 4627](https://www.github.com/boykopovar/AnyPS5/pull/4627) | `core/libs/prx/libc/CMakeLists.txt`<br>`core/libs/tests/Formatting.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/CMakeLists.txt`<br>`core/libs/tests/Formatting.cpp`<br>`docs/dev/debt/libc.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libc/CMakeLists.txt`<br>`core/libs/tests/Formatting.cpp` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `docs/dev/debt/libc.md` | | [PR 6819](https://www.github.com/boykopovar/AnyPS5/pull/6819) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `docs/dev/debt/libc.md` | | [PR 7081](https://www.github.com/boykopovar/AnyPS5/pull/7081) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libc/CMakeLists.txt`<br>`core/libs/tests/Formatting.cpp` | | [PR 7447](https://www.github.com/boykopovar/AnyPS5/pull/7447) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 7648](https://www.github.com/boykopovar/AnyPS5/pull/7648) | `docs/dev/debt/libc.md` | | [PR 7715](https://www.github.com/boykopovar/AnyPS5/pull/7715) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 8150](https://www.github.com/boykopovar/AnyPS5/pull/8150) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 8520](https://www.github.com/boykopovar/AnyPS5/pull/8520) | `core/libs/prx/libc/CMakeLists.txt`<br>`docs/dev/debt/libc.md` | | [PR 8546](https://www.github.com/boykopovar/AnyPS5/pull/8546) | `docs/dev/debt/libc.md` | | [PR 8550](https://www.github.com/boykopovar/AnyPS5/pull/8550) | `core/libs/prx/libc/src/Formatting.cpp`<br>`docs/dev/debt/libc.md` | | [PR 8658](https://www.github.com/boykopovar/AnyPS5/pull/8658) | `core/libs/prx/libc/CMakeLists.txt`<br>`core/libs/tests/Formatting.cpp` |
adriadam10 commented 2026-10-21 11:10:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T11:10:03Z.


Re-checked against current main: the new stream-scanf and vfprintf changes don't touch the sscanf/vsscanf/sscanf_s paths here, the merge is clean, and fscanf/vfscanf still throw for wide conversions through RejectGuestWideScan; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6314475931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7525#issuecomment-6314475931). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T11:10:03Z. --- Re-checked against current main: the new stream-scanf and `vfprintf` changes don't touch the `sscanf`/`vsscanf`/`sscanf_s` paths here, the merge is clean, and `fscanf`/`vfscanf` still throw for wide conversions through `RejectGuestWideScan`; merged.
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!7525
No description provided.