fix(libc): stop wcstombs before multibyte character exceeding capacity #21244

Merged
gurtgg merged 1 commit from upstream-pr/21244 into main 2026-11-16 08:27:03 +00:00
gurtgg commented 2026-11-16 01:31:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gurtgg on 2026-11-16T01:31:27Z.


What

Forward wcstombs_nid_postfix directly to wcsrtombs_nid_postfix(destination, &source, capacity, nullptr) in WideConversion.cpp. Previously, wcstombs_nid_postfix performed a manual byte-by-byte copy without verifying that multi-byte UTF-8 sequences fit completely in the remaining destination capacity, causing partial leading UTF-8 bytes to be written when a 2-byte or 3-byte wide character exceeded the remaining buffer space. Removing the unused <climits> header cleans up the include list.

Extend GuestWcsrtombs.cpp unit tests to verify wcstombs_nid_postfix: full ASCII conversion with null terminator, errno preservation, zero-capacity destination buffer handling, exact-capacity truncation without null terminator, null destination length queries on ASCII and multi-byte strings, 2-byte and 3-byte UTF-8 character conversion, and clean stoppage before multi-byte characters that exceed remaining capacity without writing partial lead bytes.

Tested

  • Added unit tests in GuestWcsrtombs.cpp covering ASCII, zero capacity, exact capacity, length query, 2-byte UTF-8 (U+0100), 3-byte UTF-8 (U+20AC), and partial multi-byte character boundary stoppage.

Checklist

  • Conventional Commit message format adhered to
  • Code adheres to repository style conventions and formatting guidelines
  • No comments introduced in source or test changes
  • Line endings are LF and files are UTF-8 without BOM
  • Unit tests added covering all touched conversion paths
  • AI-assisted: yes
  • Depends on: none
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5329469274 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21244). Originally posted by [gurtgg](https://github.com/gurtgg) on 2026-11-16T01:31:27Z. --- ### What Forward `wcstombs_nid_postfix` directly to `wcsrtombs_nid_postfix(destination, &source, capacity, nullptr)` in `WideConversion.cpp`. Previously, `wcstombs_nid_postfix` performed a manual byte-by-byte copy without verifying that multi-byte UTF-8 sequences fit completely in the remaining destination capacity, causing partial leading UTF-8 bytes to be written when a 2-byte or 3-byte wide character exceeded the remaining buffer space. Removing the unused `<climits>` header cleans up the include list. Extend `GuestWcsrtombs.cpp` unit tests to verify `wcstombs_nid_postfix`: full ASCII conversion with null terminator, errno preservation, zero-capacity destination buffer handling, exact-capacity truncation without null terminator, null destination length queries on ASCII and multi-byte strings, 2-byte and 3-byte UTF-8 character conversion, and clean stoppage before multi-byte characters that exceed remaining capacity without writing partial lead bytes. ### Tested - Added unit tests in `GuestWcsrtombs.cpp` covering ASCII, zero capacity, exact capacity, length query, 2-byte UTF-8 (`U+0100`), 3-byte UTF-8 (`U+20AC`), and partial multi-byte character boundary stoppage. ### Checklist - [x] Conventional Commit message format adhered to - [x] Code adheres to repository style conventions and formatting guidelines - [x] No comments introduced in source or test changes - [x] Line endings are LF and files are UTF-8 without BOM - [x] Unit tests added covering all touched conversion paths - [x] AI-assisted: yes - [x] Depends on: none
PrivacyOnion commented 2026-11-16 01:40:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T01:40:45Z.

Review: CHANGES_REQUESTED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 2016c41142:

  • Conventions: tools/check_conventions.py passed; git diff --check reported whitespace errors.
  • Build: guest_wcsrtombs_tests built cleanly.
  • CTest (guest_wcsrtombs):
    1/1 Test #1267: guest_wcsrtombs ..................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5914122476 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21244#pullrequestreview-5914122476). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T01:40:45Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `2016c41142`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` reported whitespace errors. - **Build**: `guest_wcsrtombs_tests` built cleanly. - **CTest** (`guest_wcsrtombs`): ``` 1/1 Test #1267: guest_wcsrtombs .................. Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` Requesting changes for the points above.
MarcZX10 commented 2026-11-16 02:11:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T02:11:45Z.

Review: APPROVED


Reviewed — looks good

The reviewed change at 2016c411424a4833787b37a1f0b945129b006b7e delegates wcstombs to the existing conversion routine, which stops before a multibyte character that does not fit; no verified blocking defect was found.

What I ran

No build or tests were run. I read the prepared description, commit list, manifest, diff, and both changed files, plus the implementation of wcsrtombs_nid_postfix used by the change.

Results

Check Result Note
Changed code review passed Both changed files read
Build and unit tests not run No execution performed
Repository prechecks not verified Author-reported only

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build and unit test execution were not performed. Author-reported prechecks and their outputs were not independently verified. No files were excluded by the prepared manifest; both changed files were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5914286960 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21244#pullrequestreview-5914286960). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T02:11:45Z. **Review:** APPROVED --- # Reviewed — looks good The reviewed change at `2016c411424a4833787b37a1f0b945129b006b7e` delegates `wcstombs` to the existing conversion routine, which stops before a multibyte character that does not fit; no verified blocking defect was found. ## What I ran No build or tests were run. I read the prepared description, commit list, manifest, diff, and both changed files, plus the implementation of `wcsrtombs_nid_postfix` used by the change. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Both changed files read | | Build and unit tests | not run | No execution performed | | Repository prechecks | not verified | Author-reported only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Build and unit test execution were not performed. Author-reported prechecks and their outputs were not independently verified. No files were excluded by the prepared manifest; both changed files were read. ## To get this to a good state Nothing is required.
PrivacyOnion commented 2026-11-16 04:00:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T04:00:49Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 6fd6699ad3:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_wcsrtombs_tests built cleanly.
  • CTest (guest_wcsrtombs):
    1/1 Test #1268: guest_wcsrtombs ..................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/WideConversion.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1268: guest_wcsrtombs ..................***Failed    0.02 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified wcstombs_nid_postfix in core/libs/prx/libc/src/WideConversion.cpp delegates to wcsrtombs_nid_postfix(destination, &source, capacity, nullptr), passing the address of the local by-value source pointer so caller pointers are not mutated while reusing full multibyte capacity checks.
    • Checked that removing unused in core/libs/prx/libc/src/WideConversion.cpp leaves all required headers (, , Api.hpp, VabiMacros.hpp) intact and builds cleanly.
    • Verified unit tests in core/libs/tests/GuestWcsrtombs.cpp cover full ASCII conversion, errno preservation, zero capacity, exact capacity without terminator, nullptr destination length queries, 2-byte (U+0100) and 3-byte (U+20AC) UTF-8 sequences, and stopping before multibyte characters that exceed remaining capacity.
    • Confirmed strict compliance with AGENTS.md rules, LP64/LLP64 type safety, and mechanical convention gates.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5914878256 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21244#pullrequestreview-5914878256). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T04:00:49Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `6fd6699ad3`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_wcsrtombs_tests` built cleanly. - **CTest** (`guest_wcsrtombs`): ``` 1/1 Test #1268: guest_wcsrtombs .................. Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/WideConversion.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1268: guest_wcsrtombs ..................***Failed 0.02 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified wcstombs_nid_postfix in core/libs/prx/libc/src/WideConversion.cpp delegates to wcsrtombs_nid_postfix(destination, &source, capacity, nullptr), passing the address of the local by-value source pointer so caller pointers are not mutated while reusing full multibyte capacity checks. - Checked that removing unused <climits> in core/libs/prx/libc/src/WideConversion.cpp leaves all required headers (<cstddef>, <cstdint>, Api.hpp, VabiMacros.hpp) intact and builds cleanly. - Verified unit tests in core/libs/tests/GuestWcsrtombs.cpp cover full ASCII conversion, errno preservation, zero capacity, exact capacity without terminator, nullptr destination length queries, 2-byte (U+0100) and 3-byte (U+20AC) UTF-8 sequences, and stopping before multibyte characters that exceed remaining capacity. - Confirmed strict compliance with AGENTS.md rules, LP64/LLP64 type safety, and mechanical convention gates. Looks Good To Me
MarcZX10 commented 2026-11-16 05:01:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T05:01:59Z.

Review: APPROVED


Reviewed — looks good

The change at head 6fd6699ad3c4f5194a11f135251764ed7df74984 routes wcstombs_nid_postfix through the existing whole-character conversion and adds coverage for capacity boundaries; no blocking defect is apparent in the changed code.

What I ran

Read the prepared description, commit summary, manifest, diff, both changed files, and the existing wcsrtombs_nid_postfix implementation in Multibyte.cpp. I did not run tests or other commands.

Results

Check Result Note
Changed code review passed Both changed files read
Existing conversion behavior passed Whole-character capacity check inspected
Tests not run No test command run

Findings

Blocking

None.

Non-blocking

None.

Not verified

Tests were not run. No excluded files were listed in the available manifest; files.json was not available through the reader. No other repository files were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5915248851 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21244#pullrequestreview-5915248851). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T05:01:59Z. **Review:** APPROVED --- # Reviewed — looks good The change at head `6fd6699ad3c4f5194a11f135251764ed7df74984` routes `wcstombs_nid_postfix` through the existing whole-character conversion and adds coverage for capacity boundaries; no blocking defect is apparent in the changed code. ## What I ran Read the prepared description, commit summary, manifest, diff, both changed files, and the existing `wcsrtombs_nid_postfix` implementation in `Multibyte.cpp`. I did not run tests or other commands. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Both changed files read | | Existing conversion behavior | passed | Whole-character capacity check inspected | | Tests | not run | No test command run | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Tests were not run. No excluded files were listed in the available manifest; `files.json` was not available through the reader. No other repository files were read. ## To get this to a good state Nothing is required.
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!21244
No description provided.