fix(libc): read characters outside the BMP as surrogate pairs in fgetwc #10929

Merged
Aspct3434 merged 1 commit from upstream-pr/10929 into main 2026-10-29 12:49:55 +00:00
Aspct3434 commented 2026-10-25 10:47:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aspct3434 on 2026-10-25T10:47:13Z.


What

fputws and wprintf write a UTF-16 surrogate pair as one four-byte UTF-8 sequence, but fgetwc decoded that sequence into a single code point above 0xFFFF, which is not a 16-bit guest wchar_t value, and fgetws stored it truncated: fputws(u"A\U0001F600\n") read back as A, U+F600, newline.

fgetwc now returns the high surrogate and keeps the low one in the stream for the next read, so fgetws stores the pair, also when the buffer fills between the two halves. A successful seek discards it, as FreeBSD's fseeko resets the stream's mbstate. Sequences that decode to an overlong form or above U+10FFFF, and lead bytes F8 to FF, fail with EILSEQ instead of returning made-up characters; the three-byte surrogates that fputwc writes for a single unit are still read back as that unit.

Claude-Session: https://claude.ai/code/session_01QegaHM5QeCynUQ2ZLWHE4b

ungetwc after a high surrogate: the pushed-back character is now read before the queued low half, which follows it.

Tested

Linux and Windows 11 (WinLibs GCC 15.2.0) runs of guest_standard_streams by other testers on the PR thread; the new CheckSupplementaryWideCharacters cases, including getwc, ungetwc(L'X'), getwc, fail on main. No title tested.

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:5004027485 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10929). Originally posted by [Aspct3434](https://github.com/Aspct3434) on 2026-10-25T10:47:13Z. --- ### What fputws and wprintf write a UTF-16 surrogate pair as one four-byte UTF-8 sequence, but fgetwc decoded that sequence into a single code point above 0xFFFF, which is not a 16-bit guest wchar_t value, and fgetws stored it truncated: fputws(u"A\U0001F600\n") read back as A, U+F600, newline. fgetwc now returns the high surrogate and keeps the low one in the stream for the next read, so fgetws stores the pair, also when the buffer fills between the two halves. A successful seek discards it, as FreeBSD's fseeko resets the stream's mbstate. Sequences that decode to an overlong form or above U+10FFFF, and lead bytes F8 to FF, fail with EILSEQ instead of returning made-up characters; the three-byte surrogates that fputwc writes for a single unit are still read back as that unit. Claude-Session: https://claude.ai/code/session_01QegaHM5QeCynUQ2ZLWHE4b `ungetwc` after a high surrogate: the pushed-back character is now read before the queued low half, which follows it. ### Tested Linux and Windows 11 (WinLibs GCC 15.2.0) runs of `guest_standard_streams` by other testers on the PR thread; the new `CheckSupplementaryWideCharacters` cases, including `getwc`, `ungetwc(L'X')`, `getwc`, fail on `main`. No title tested. ### 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
MarcZX10 commented 2026-10-25 11:03:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:03:15Z.

Review: CHANGES_REQUESTED


Found one blocking stream-ordering regression in the pending-surrogate path. fgetwc stores the low surrogate after returning the high half, and the next fgetwc returns that queued unit before reading the underlying stream. If a caller then successfully calls ungetwc(X, stream), ungetwc_nid_postfix pushes X into the host stream, but the next fgetwc still returns the queued low surrogate first. That violates the pushback guarantee that the next wide-character read returns the successfully pushed-back character. Please add a regression case that reads the high half of a supplementary character, pushes back a BMP character, and verifies the order of the next reads; preserve the queued low half after the pushed-back character as appropriate.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642850580 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10929#pullrequestreview-5642850580). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:03:15Z. **Review:** CHANGES_REQUESTED --- Found one blocking stream-ordering regression in the pending-surrogate path. `fgetwc` stores the low surrogate after returning the high half, and the next `fgetwc` returns that queued unit before reading the underlying stream. If a caller then successfully calls `ungetwc(X, stream)`, `ungetwc_nid_postfix` pushes `X` into the host stream, but the next `fgetwc` still returns the queued low surrogate first. That violates the pushback guarantee that the next wide-character read returns the successfully pushed-back character. Please add a regression case that reads the high half of a supplementary character, pushes back a BMP character, and verifies the order of the next reads; preserve the queued low half after the pushed-back character as appropriate.
adriadam10 commented 2026-10-26 23:17:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-26T23:17:51Z.

Review: CHANGES_REQUESTED


Thanks, the decoding and the new CheckSupplementaryWideCharacters cases look right (they fail on main). Two things block this:

  • StandardStreams.cpp:104: after fgetwc returns the high surrogate, the queued low half is returned by the next fgetwc before anything the caller pushed back. ungetwc_nid_postfix (StandardStreams.cpp:130) only calls ungetc on the host stream and ignores lowSurrogate, so getwc (0xd83d), ungetwc(L'X'), then getwc returns 0xde00 instead of X (reproduced by @wachi35676, same point as @MarcZX10's review; C11 7.29.3.10). Fix it so the pushed-back character is read next and the low half still follows it, and add that sequence as a regression case next to line 302 of GuestStandardStreams.cpp.
  • The PR body is empty: What, Tested, Depends on, Fixes, Unblocks and the checklist and AI-assisted lines are unfilled, so Conventions fails. Please fill the existing template.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5660362059 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10929#pullrequestreview-5660362059). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-26T23:17:51Z. **Review:** CHANGES_REQUESTED --- Thanks, the decoding and the new `CheckSupplementaryWideCharacters` cases look right (they fail on `main`). Two things block this: - `StandardStreams.cpp:104`: after `fgetwc` returns the high surrogate, the queued low half is returned by the next `fgetwc` before anything the caller pushed back. `ungetwc_nid_postfix` (`StandardStreams.cpp:130`) only calls `ungetc` on the host stream and ignores `lowSurrogate`, so `getwc` (0xd83d), `ungetwc(L'X')`, then `getwc` returns 0xde00 instead of `X` (reproduced by @wachi35676, same point as @MarcZX10's review; C11 7.29.3.10). Fix it so the pushed-back character is read next and the low half still follows it, and add that sequence as a regression case next to line 302 of `GuestStandardStreams.cpp`. - The PR body is empty: `What`, `Tested`, `Depends on`, `Fixes`, `Unblocks` and the checklist and `AI-assisted` lines are unfilled, so Conventions fails. Please fill the existing template.
MarcZX10 commented 2026-10-29 06:28:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T06:28:42Z.

Review: APPROVED


Re-checked — approved

The pending-surrogate pushback issue is fixed on the current head 84aec8b0ac56f402e4072c59495abf56211be41c. FileStream now tracks successful pushbacks while a low surrogate is queued; TakeLowSurrogate() defers that low half until the pushed-back byte has been consumed, and NoteWideRead() decrements the pending count. The regression covers getwc returning the high half, ungetwc(L'X'), then reading X, the low half, and the following newline. I also checked that reposition/reopen/reset paths clear the new state. This resolves my earlier functional finding.

The author documents the required template fields and tests; Linux and Windows focused runs are reported on the PR thread, and hosted Windows/conventions/policy checks pass. The current-main merge preview is clean; source/test paths have no later main-side edits, though the shared debt entry was updated and the preview integrates it cleanly. I did not rerun the stream tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693716479 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10929#pullrequestreview-5693716479). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T06:28:42Z. **Review:** APPROVED --- ## Re-checked — approved The pending-surrogate pushback issue is fixed on the current head `84aec8b0ac56f402e4072c59495abf56211be41c`. `FileStream` now tracks successful pushbacks while a low surrogate is queued; `TakeLowSurrogate()` defers that low half until the pushed-back byte has been consumed, and `NoteWideRead()` decrements the pending count. The regression covers `getwc` returning the high half, `ungetwc(L'X')`, then reading `X`, the low half, and the following newline. I also checked that reposition/reopen/reset paths clear the new state. This resolves my earlier functional finding. The author documents the required template fields and tests; Linux and Windows focused runs are reported on the PR thread, and hosted Windows/conventions/policy checks pass. The current-main merge preview is clean; source/test paths have no later main-side edits, though the shared debt entry was updated and the preview integrates it cleanly. I did not rerun the stream tests locally.
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!10929
No description provided.