fix(libc): read characters outside the BMP as surrogate pairs in fgetwc #10929
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10929"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
ungetwcafter 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_streamsby other testers on the PR thread; the newCheckSupplementaryWideCharacterscases, includinggetwc,ungetwc(L'X'),getwc, fail onmain. No title tested.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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.
fgetwcstores the low surrogate after returning the high half, and the nextfgetwcreturns that queued unit before reading the underlying stream. If a caller then successfully callsungetwc(X, stream),ungetwc_nid_postfixpushesXinto the host stream, but the nextfgetwcstill 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.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-26T23:17:51Z.
Review: CHANGES_REQUESTED
Thanks, the decoding and the new
CheckSupplementaryWideCharacterscases look right (they fail onmain). Two things block this:StandardStreams.cpp:104: afterfgetwcreturns the high surrogate, the queued low half is returned by the nextfgetwcbefore anything the caller pushed back.ungetwc_nid_postfix(StandardStreams.cpp:130) only callsungetcon the host stream and ignoreslowSurrogate, sogetwc(0xd83d),ungetwc(L'X'), thengetwcreturns 0xde00 instead ofX(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 ofGuestStandardStreams.cpp.What,Tested,Depends on,Fixes,Unblocksand the checklist andAI-assistedlines are unfilled, so Conventions fails. Please fill the existing template.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.FileStreamnow tracks successful pushbacks while a low surrogate is queued;TakeLowSurrogate()defers that low half until the pushed-back byte has been consumed, andNoteWideRead()decrements the pending count. The regression coversgetwcreturning the high half,ungetwc(L'X'), then readingX, 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.