fix(libc): convert wcsrtombs using guest characters #1507

Merged
ArjenS-LA merged 1 commit from upstream-pr/1507 into main 2026-10-10 01:05:56 +00:00
ArjenS-LA commented 2026-10-08 10:26:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ArjenS-LA on 2026-10-08T10:26:31Z.


What

wcsrtombs passed guest strings and conversion state directly to the host CRT. On Windows, an invalid character such as U+0100 returned host EILSEQ=42 instead of guest 86. On Linux, the host's 32-bit wchar_t also disagrees with the guest's 16-bit characters.

The export now lives beside the other guest multibyte conversions and uses wcrtomb with 16-bit guest characters. Partial conversions leave the source at the next unconverted character; a stored terminator sets it to null. Length queries ignore capacity and preserve the source pointer. Invalid characters report guest EILSEQ and preserve the converted prefix.

Adds the registered guest_wcsrtombs regression test and updates the existing technical-debt entry. Conversion follows the existing guest single-byte C locale; other encodings and shift states remain unsupported. The restart and length-query behavior follows the C working draft, section 7.31.6.4.2.

Tested

Windows validation of PR head 74e1a6f5c4ba8c1f4cf805ba23965199bd473407, rebased onto main 6b56175a4290a03ab28ebc6697beeffe6fe9be8d.

The rebase resolves the TechnicalDebt.md conflict by preserving upstream's SPIRV-Tools and Windows host-import entries alongside the corrected wide-character note. The note's leading link now points to Multibyte.cpp as requested in review. The implementation and regression-test patch are unchanged.

Windows 11 Enterprise 10.0.26200; WinLibs MinGW-w64 GCC 15.2.0 r2 UCRT/posix/SEH, Ninja Release, BUILD_TESTING=ON, SPIRV-Tools disabled. This uses the installed r2 toolchain, not CI's r7 package.

The libc library and all four selected test targets rebuilt successfully after the rebase:

CTest test Result
guest_wcsrtombs Passed
guest_wcsrtombs_s Passed
guest_wide_strings Passed
guest_locale Passed

4/4 passed, 0 failed, 0 skipped. The new test covers full, empty, partial and resumed conversions; exact and zero capacity; high-byte characters; explicit and null state pointers; length queries; source-pointer positions; output boundaries; and guest error numbers.

Before the fix, the new regression test was compiled directly against the original Strings.cpp from 8763483811950f6e150201e664e771ec0dac0794, with a test-only errno accessor. It failed at invalid character uses guest EILSEQ. The same standalone test passed after the replacement. This is a focused reproduction, not a full baseline-versus-PR suite comparison.

The committed-head conventions check, diff whitespace check, and test-registration check passed. The registration check found no newly unregistered test executables.

These local results cover the pinned PR revision. Linux, the full registered CTest suite, the complete default/libs build, games, and the eventual merged tree were not rerun locally for this rebase. No performance or game-compatibility conclusion is claimed.

Independent validation of the earlier head: Dextroid17 reported 462/462 Linux tests passing after integrating it with main fd5342b6 (CachyOS, GCC 16.2.1, Release, SPIRV-Tools enabled). Reverting only the implementation made the new test fail at full conversion length. Those are the reviewer's results for that earlier integration, not a Linux run of the rebased head above.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes — OpenAI Codex assisted with implementation, regression tests, execution, analysis, and this report.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4785349089 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1507). Originally posted by [ArjenS-LA](https://github.com/ArjenS-LA) on 2026-10-08T10:26:31Z. --- ### What `wcsrtombs` passed guest strings and conversion state directly to the host CRT. On Windows, an invalid character such as U+0100 returned host `EILSEQ=42` instead of guest `86`. On Linux, the host's 32-bit `wchar_t` also disagrees with the guest's 16-bit characters. The export now lives beside the other guest multibyte conversions and uses `wcrtomb` with 16-bit guest characters. Partial conversions leave the source at the next unconverted character; a stored terminator sets it to null. Length queries ignore capacity and preserve the source pointer. Invalid characters report guest `EILSEQ` and preserve the converted prefix. Adds the registered `guest_wcsrtombs` regression test and updates the existing technical-debt entry. Conversion follows the existing guest single-byte C locale; other encodings and shift states remain unsupported. The restart and length-query behavior follows the [C working draft, section 7.31.6.4.2](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf). ### Tested Windows validation of PR head `74e1a6f5c4ba8c1f4cf805ba23965199bd473407`, rebased onto main `6b56175a4290a03ab28ebc6697beeffe6fe9be8d`. The rebase resolves the TechnicalDebt.md conflict by preserving upstream's SPIRV-Tools and Windows host-import entries alongside the corrected wide-character note. The note's leading link now points to Multibyte.cpp as requested in review. The implementation and regression-test patch are unchanged. Windows 11 Enterprise 10.0.26200; WinLibs MinGW-w64 GCC 15.2.0 r2 UCRT/posix/SEH, Ninja Release, `BUILD_TESTING=ON`, SPIRV-Tools disabled. This uses the installed r2 toolchain, not CI's r7 package. The libc library and all four selected test targets rebuilt successfully after the rebase: | CTest test | Result | | --- | --- | | `guest_wcsrtombs` | Passed | | `guest_wcsrtombs_s` | Passed | | `guest_wide_strings` | Passed | | `guest_locale` | Passed | **4/4 passed, 0 failed, 0 skipped.** The new test covers full, empty, partial and resumed conversions; exact and zero capacity; high-byte characters; explicit and null state pointers; length queries; source-pointer positions; output boundaries; and guest error numbers. Before the fix, the new regression test was compiled directly against the original `Strings.cpp` from `8763483811950f6e150201e664e771ec0dac0794`, with a test-only errno accessor. It failed at `invalid character uses guest EILSEQ`. The same standalone test passed after the replacement. This is a focused reproduction, not a full baseline-versus-PR suite comparison. The committed-head conventions check, diff whitespace check, and test-registration check passed. The registration check found no newly unregistered test executables. These local results cover the pinned PR revision. Linux, the full registered CTest suite, the complete default/libs build, games, and the eventual merged tree were not rerun locally for this rebase. No performance or game-compatibility conclusion is claimed. Independent validation of the earlier head: [Dextroid17 reported](https://github.com/boykopovar/AnyPS5/pull/1507#issuecomment-6060964678) 462/462 Linux tests passing after integrating it with main `fd5342b6` (CachyOS, GCC 16.2.1, Release, SPIRV-Tools enabled). Reverting only the implementation made the new test fail at `full conversion length`. Those are the reviewer's results for that earlier integration, not a Linux run of the rebased head above. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes — OpenAI Codex assisted with implementation, regression tests, execution, analysis, and this report.
klosixgaming-arch commented 2026-10-08 10:55:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T10:55:21Z.

Review: COMMENTED


Solid fix for wcsrtombs with excellent test coverage. The comprehensive edge case testing (partial conversions, exact capacity, invalid characters, length queries) is thorough and follows C11 standards properly.

Note: This overlaps with #1511 and #1512 on the TechnicalDebt.md line - all three address wide-character conversion issues. Coordination between these PRs would be helpful to avoid merge conflicts.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5455478296 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1507#pullrequestreview-5455478296). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-08T10:55:21Z. **Review:** COMMENTED --- Solid fix for wcsrtombs with excellent test coverage. The comprehensive edge case testing (partial conversions, exact capacity, invalid characters, length queries) is thorough and follows C11 standards properly. Note: This overlaps with #1511 and #1512 on the TechnicalDebt.md line - all three address wide-character conversion issues. Coordination between these PRs would be helpful to avoid merge conflicts.
klosixgaming-arch commented 2026-10-08 11:26:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:26:41Z.

Review: APPROVED


Good implementation of wcsrtombs using the guest's single-byte C locale through wcrtomb. This fixes the issue where on Linux (where host wchar_t is 32 bits), guest strings were misread and miswritten.

Strengths:

  • Clean implementation using existing wcrtomb infrastructure
  • Comprehensive test coverage including edge cases (partial conversion, invalid characters, zero capacity, length queries)
  • Proper error handling with guest EILSEQ errno value (86)
  • Good documentation update in TechnicalDebt.md noting remaining limitations

Technical notes:

  • The approach of iterating through wide characters and converting each individually is correct for the single-byte C locale
  • Proper handling of source pointer updates on both success and failure
  • Test cases properly verify all conversion scenarios including resume after partial conversion

This will improve compatibility with titles that use wcsrtombs. Ready to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5455824916 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1507#pullrequestreview-5455824916). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-08T11:26:41Z. **Review:** APPROVED --- Good implementation of `wcsrtombs` using the guest's single-byte C locale through `wcrtomb`. This fixes the issue where on Linux (where host wchar_t is 32 bits), guest strings were misread and miswritten. **Strengths:** - Clean implementation using existing `wcrtomb` infrastructure - Comprehensive test coverage including edge cases (partial conversion, invalid characters, zero capacity, length queries) - Proper error handling with guest EILSEQ errno value (86) - Good documentation update in TechnicalDebt.md noting remaining limitations **Technical notes:** - The approach of iterating through wide characters and converting each individually is correct for the single-byte C locale - Proper handling of source pointer updates on both success and failure - Test cases properly verify all conversion scenarios including resume after partial conversion This will improve compatibility with titles that use `wcsrtombs`. Ready to merge.
Dextroid17 commented 2026-10-08 13:30:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T13:30:02Z.


Ran this on Linux because it has no independent run yet, and it holds up. Details, so the next reader does not have to redo them.

Merged onto current main (fd5342b6): one conflict, in docs/dev/TechnicalDebt.md, and only because main added an unrelated SPIRV-Tools entry next to the one you edited — keep both.

Result (CachyOS, GCC 16.2.1, Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON):

ctest -R 'guest_wcsrtombs|guest_wcsrtombs_s|guest_strings'   →  3/3 passed
full suite                                                   →  462/462 passed

The test bites — reverted only the implementation (Strings.cpp and Multibyte.cpp back to main, test and registration kept):

Test #33: guest_wcsrtombs ...***Failed
full conversion length

That failure is worth quoting in the description, because it is stronger than "misreads non-ASCII": on main the very first assertion fails, so every guest string of two or more characters fails to convert, not only the ones with a character above 0x7f. A change that fixes every caller rather than one title's case is the shape that gets merged here.

Semantics cross-checked against the platform's own wcsrtombs (same cases, C locale, host wchar_t, so the reference is the same function implemented correctly):

capacity 8   → 3, source NULL, terminator stored
capacity 4   → 3, source NULL
capacity 3   → 3, source at the terminator      (terminator pending)
capacity 2   → 2, source at the third character
capacity 0   → 0, source unchanged
dst NULL     → the length, source unchanged
invalid char → (size_t)-1, source at the offending character, errno set

Every one matches yours, including the two that are easy to get wrong (the terminator-not-stored case and the length query leaving source alone). The errno half is complete through wcrtomb, which sets 86 itself — I checked, because the plain function has to set it and it is not obvious from the call site.

One nit: the debt entry's first link still points at Strings.cpp although wcsrtombs now lives in Multibyte.cpp; pointing that link at Multibyte.cpp (and leaving the swprintf one) would match the tree after this change.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6060964678 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1507#issuecomment-6060964678). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T13:30:02Z. --- Ran this on Linux because it has no independent run yet, and it holds up. Details, so the next reader does not have to redo them. **Merged onto current `main` (`fd5342b6`):** one conflict, in `docs/dev/TechnicalDebt.md`, and only because `main` added an unrelated SPIRV-Tools entry next to the one you edited — keep both. **Result** (CachyOS, GCC 16.2.1, Release, `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`): ``` ctest -R 'guest_wcsrtombs|guest_wcsrtombs_s|guest_strings' → 3/3 passed full suite → 462/462 passed ``` **The test bites — reverted only the implementation** (`Strings.cpp` and `Multibyte.cpp` back to `main`, test and registration kept): ``` Test #33: guest_wcsrtombs ...***Failed full conversion length ``` That failure is worth quoting in the description, because it is stronger than "misreads non-ASCII": on `main` the very first assertion fails, so **every guest string of two or more characters** fails to convert, not only the ones with a character above `0x7f`. A change that fixes every caller rather than one title's case is the shape that gets merged here. **Semantics cross-checked against the platform's own `wcsrtombs`** (same cases, C locale, host `wchar_t`, so the reference is the same function implemented correctly): ``` capacity 8 → 3, source NULL, terminator stored capacity 4 → 3, source NULL capacity 3 → 3, source at the terminator (terminator pending) capacity 2 → 2, source at the third character capacity 0 → 0, source unchanged dst NULL → the length, source unchanged invalid char → (size_t)-1, source at the offending character, errno set ``` Every one matches yours, including the two that are easy to get wrong (the terminator-not-stored case and the length query leaving `source` alone). The `errno` half is complete through `wcrtomb`, which sets `86` itself — I checked, because the plain function has to set it and it is not obvious from the call site. **One nit:** the debt entry's first link still points at `Strings.cpp` although `wcsrtombs` now lives in `Multibyte.cpp`; pointing that link at `Multibyte.cpp` (and leaving the `swprintf` one) would match the tree after this change. Written with AI assistance.
hashaam1217 commented 2026-10-08 19:54:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hashaam1217 on 2026-10-08T19:54:50Z.


Linux run, since the bug is Linux-specific (32-bit host wchar_t): Ubuntu 24.04.5, glibc 2.39, GCC 13.3.0, Release. Head 74e1a6f5 merged onto main 4b6ab6bb.

  • guest_wcsrtombs, guest_wcsrtombs_s, guest_wide_strings and guest_locale pass.
  • I reverted Strings.cpp and Multibyte.cpp to main and kept the new test. On Linux, guest_wcsrtombs then fails at its first check, full conversion length, not at the EILSEQ check where it failed on Windows. So on Linux the old host-wchar_t path already gets the plain ASCII conversion wrong.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6067901677 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1507#issuecomment-6067901677). Originally posted by [hashaam1217](https://github.com/hashaam1217) on 2026-10-08T19:54:50Z. --- Linux run, since the bug is Linux-specific (32-bit host `wchar_t`): Ubuntu 24.04.5, glibc 2.39, GCC 13.3.0, Release. Head 74e1a6f5 merged onto main 4b6ab6bb. - `guest_wcsrtombs`, `guest_wcsrtombs_s`, `guest_wide_strings` and `guest_locale` pass. - I reverted `Strings.cpp` and `Multibyte.cpp` to main and kept the new test. On Linux, `guest_wcsrtombs` then fails at its first check, `full conversion length`, not at the EILSEQ check where it failed on Windows. So on Linux the old host-`wchar_t` path already gets the plain ASCII conversion wrong.
github-actions[bot] commented 2026-10-08 20:16:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T20:16:14Z.


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
#545 core/libs/prx/libc/src/Strings.cpp L455-458
#1511 docs/dev/TechnicalDebt.md L259
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6068250766 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1507#issuecomment-6068250766). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T20:16:14Z. --- <!-- 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 | |---|---|---|---| | #545 | `core/libs/prx/libc/src/Strings.cpp` [L455-458](https://github.com/boykopovar/AnyPS5/blob/7703fda15e4ab4025dd817eae5964315e6dd134b/core/libs/prx/libc/src/Strings.cpp#L455-L458) | | | | #1511 | `docs/dev/TechnicalDebt.md` [L259](https://github.com/boykopovar/AnyPS5/blob/7703fda15e4ab4025dd817eae5964315e6dd134b/docs/dev/TechnicalDebt.md#L259) | | |
adriadam10 commented 2026-10-10 01:05:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T01:05:53Z.


Checked the build and the four libc wide-string tests (guest_wcsrtombs, guest_wcsrtombs_s, guest_wide_strings, guest_locale) on Linux; guest_wcsrtombs fails without the implementation. Restart, length-query and EILSEQ paths match the description; TechnicalDebt entry is accurate. CI is green; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6091977432 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1507#issuecomment-6091977432). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T01:05:53Z. --- Checked the build and the four libc wide-string tests (`guest_wcsrtombs`, `guest_wcsrtombs_s`, `guest_wide_strings`, `guest_locale`) on Linux; `guest_wcsrtombs` fails without the implementation. Restart, length-query and EILSEQ paths match the description; TechnicalDebt entry is accurate. CI is green; merging.
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!1507
No description provided.