fix(libc): follow the console fgetws for short buffers and read errors #9596

Merged
JJenkx merged 1 commit from upstream-pr/9596 into main 2026-10-24 02:22:32 +00:00
JJenkx commented 2026-10-22 03:57:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T03:57:03Z.


What

fgetws now follows the console's fgetws in two cases where it differed. The console code was read from the libc.prx that titles ship in sce_module/. I read it from disassembly, not by measuring on a console. The copies of PPSA13934, PPSA07923 and PPSA05916 have the same 133-byte fgetws (F81hKe2k2tg). The addresses below are from PPSA13934's copy (fgetws 0x6cab0, fgetwc 0x6c7a0). The reading is also posted on #8897 (comment).

Case Console Before Now
size < 2 Returns null at once and writes nothing: no buffer, errno or stream flag (cmp $0x2,%esi; jge …, else xor %r14d,%r14d). size == 1 stored a NUL and returned the buffer. size <= 0 set the stream error and errno 22. Returns null and writes nothing.
fgetwc returns -1 after some characters were stored Stores the NUL and returns the buffer. Null only when nothing was stored (cmp %r14,%rbx; je). It never checks ferror. Returned null when ferror was set. Returns the terminated buffer.

Already matching, unchanged:

  • Each fgetwc result is stored as its low 16 bits (mov %ax,(%rbx)).
  • The newline test compares all 32 bits (cmp $0xa,%eax).

This means the 32-to-16-bit narrowing reported in #8897 is the console's own behaviour, not a loss in this implementation.

Not reproduced, listed in the debt entry:

  • A null buffer or stream: the console dereferences it, and here it keeps failing with errno 22.
  • The calls the console makes before and after the read loop.
  • The call it makes when bits 0xc of the stream's second flag byte are set.

Changed tests: three guest_standard_streams assertions encoded the old guesses, and now expect the console results:

  • sizes 1, 0 and -1
  • an invalid byte after a stored character, which now gives the buffer "A"

The fgetwc error-indicator check after that invalid byte is unchanged.

Tested

  • Platform: Windows 11 (10.0.22621), WinLibs GCC 15.2.0 UCRT r7, Release, BUILD_TESTING=ON.
  • Results: guest_standard_streams, guest_standard_streams_closed_stdout and guest_standard_streams_exclusive_stdout pass, as do guest_wide_formatting, guest_wide_strings, guest_wide_conversion and guest_wide_stream_scan.
  • Conventions: tools/check_conventions.py passes.
  • Rebased onto main b786b907c8 (2026-10-22), head 5ada941529: guest_standard_streams, guest_standard_streams_closed_stdout, guest_standard_streams_exclusive_stdout and the other libc stream, wide-character and formatting tests (26) pass in three runs out of three. check_conventions.py and git diff --check pass. With #9342, libc.md conflicts only on adjacency: #9342's new gets entry goes directly above this PR's fgetws entry, and both lines are kept unchanged.
  • Not done: no title was run, and nothing was measured on a console.

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 (related: #8897)
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4961863989 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9596). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T03:57:03Z. --- ### What `fgetws` now follows the console's `fgetws` in two cases where it differed. The console code was read from the `libc.prx` that titles ship in `sce_module/`. I read it from disassembly, not by measuring on a console. The copies of PPSA13934, PPSA07923 and PPSA05916 have the same 133-byte `fgetws` (`F81hKe2k2tg`). The addresses below are from PPSA13934's copy (`fgetws` 0x6cab0, `fgetwc` 0x6c7a0). The reading is also posted on #8897 ([comment](https://github.com/boykopovar/AnyPS5/issues/8897#issuecomment-6332210691)). | Case | Console | Before | Now | |---|---|---|---| | `size < 2` | Returns null at once and writes nothing: no buffer, `errno` or stream flag (`cmp $0x2,%esi; jge …`, else `xor %r14d,%r14d`). | `size == 1` stored a NUL and returned the buffer. `size <= 0` set the stream error and `errno` 22. | Returns null and writes nothing. | | `fgetwc` returns -1 after some characters were stored | Stores the NUL and returns the buffer. Null only when nothing was stored (`cmp %r14,%rbx; je`). It never checks `ferror`. | Returned null when `ferror` was set. | Returns the terminated buffer. | **Already matching, unchanged:** - Each `fgetwc` result is stored as its low 16 bits (`mov %ax,(%rbx)`). - The newline test compares all 32 bits (`cmp $0xa,%eax`). This means the 32-to-16-bit narrowing reported in #8897 is the console's own behaviour, not a loss in this implementation. **Not reproduced, listed in the debt entry:** - A null buffer or stream: the console dereferences it, and here it keeps failing with `errno` 22. - The calls the console makes before and after the read loop. - The call it makes when bits `0xc` of the stream's second flag byte are set. **Changed tests:** three `guest_standard_streams` assertions encoded the old guesses, and now expect the console results: - sizes 1, 0 and -1 - an invalid byte after a stored character, which now gives the buffer `"A"` The `fgetwc` error-indicator check after that invalid byte is unchanged. ### Tested - **Platform:** Windows 11 (10.0.22621), WinLibs GCC 15.2.0 UCRT r7, Release, `BUILD_TESTING=ON`. - **Results:** `guest_standard_streams`, `guest_standard_streams_closed_stdout` and `guest_standard_streams_exclusive_stdout` pass, as do `guest_wide_formatting`, `guest_wide_strings`, `guest_wide_conversion` and `guest_wide_stream_scan`. - **Conventions:** `tools/check_conventions.py` passes. - **Rebased** onto `main` b786b907c8 (2026-10-22), head 5ada941529: `guest_standard_streams`, `guest_standard_streams_closed_stdout`, `guest_standard_streams_exclusive_stdout` and the other libc stream, wide-character and formatting tests (26) pass in three runs out of three. `check_conventions.py` and `git diff --check` pass. With #9342, `libc.md` conflicts only on adjacency: #9342's new `gets` entry goes directly above this PR's `fgetws` entry, and both lines are kept unchanged. - **Not done:** no title was run, and nothing was measured on a console. ### 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 (related: #8897) - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-22 04:07:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:07:53Z.

Review: APPROVED


The revised fgetws handling matches the documented disassembly cases: sizes below two return before touching stream/buffer state, and a read error after at least one character returns a terminated buffer while retaining the stream error flag. The tests cover both regressions and preserve the neighboring wide-stream cases. The author clearly labels the source as disassembly rather than console measurement and records the remaining ABI questions in debt. Hosted conventions, eligibility, intake, labels, report, register-context, relinker matrix, ASAN and write-watch checks pass; hosted Linux/Windows product builds are skipped. The Windows focused suite is author-reported. I did not run tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609141590 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9596#pullrequestreview-5609141590). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:07:53Z. **Review:** APPROVED --- The revised `fgetws` handling matches the documented disassembly cases: sizes below two return before touching stream/buffer state, and a read error after at least one character returns a terminated buffer while retaining the stream error flag. The tests cover both regressions and preserve the neighboring wide-stream cases. The author clearly labels the source as disassembly rather than console measurement and records the remaining ABI questions in debt. Hosted conventions, eligibility, intake, labels, report, register-context, relinker matrix, ASAN and write-watch checks pass; hosted Linux/Windows product builds are skipped. The Windows focused suite is author-reported. I did not run tests locally.
MarcZX10 commented 2026-10-22 09:59:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T09:59:54Z.

Review: CHANGES_REQUESTED


The fgetws changes match the cited bundled-libc disassembly: sizes below two return before touching the buffer, errno or stream, and a read error after stored characters yields a terminated buffer while preserving the stream error. The tests cover those cases; the debt entry distinguishes bundled-binary disassembly from console measurement and retains the unresolved calls/decoding details. Exact-head git diff --check and conventions pass. Hosted conventions, eligibility, intake, labels, report, relinker matrix, ASan, register-context and write-watch pass; hosted Linux/Windows product builds are skipped. The author reports the focused Windows tests. I did not run runtime tests locally. I need to correct my earlier approval against current repository state. This branch is 91 commits behind current main (01b38ec is based on 6ef5a1c), so the code and tests have not been revalidated on the current integration base. Please rebase and rerun the focused standard-stream tests, then update the tested-head/base information. The bundled-binary source is useful evidence for behavior, but does not itself establish a current title stop. The overlap bot flags #9342 on the adjacent docs/dev/debt/libc.md hunk. I checked its patch: it adds the separate gets entry immediately before this PR's fgetws entry; there is no implementation overlap. Please coordinate/rebase the debt edits with that author and declare a dependency only if an actual merge order is needed. Fixes: none (related: #8897) remains accurate because the reported supplementary-character question is not resolved by these two behavior changes.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5612441722 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9596#pullrequestreview-5612441722). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T09:59:54Z. **Review:** CHANGES_REQUESTED --- The `fgetws` changes match the cited bundled-libc disassembly: sizes below two return before touching the buffer, errno or stream, and a read error after stored characters yields a terminated buffer while preserving the stream error. The tests cover those cases; the debt entry distinguishes bundled-binary disassembly from console measurement and retains the unresolved calls/decoding details. Exact-head `git diff --check` and conventions pass. Hosted conventions, eligibility, intake, labels, report, relinker matrix, ASan, register-context and write-watch pass; hosted Linux/Windows product builds are skipped. The author reports the focused Windows tests. I did not run runtime tests locally. I need to correct my earlier approval against current repository state. This branch is 91 commits behind current `main` (`01b38ec` is based on `6ef5a1c`), so the code and tests have not been revalidated on the current integration base. Please rebase and rerun the focused standard-stream tests, then update the tested-head/base information. The bundled-binary source is useful evidence for behavior, but does not itself establish a current title stop. The overlap bot flags #9342 on the adjacent `docs/dev/debt/libc.md` hunk. I checked its patch: it adds the separate `gets` entry immediately before this PR's `fgetws` entry; there is no implementation overlap. Please coordinate/rebase the debt edits with that author and declare a dependency only if an actual merge order is needed. `Fixes: none (related: #8897)` remains accurate because the reported supplementary-character question is not resolved by these two behavior changes. I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
JJenkx commented 2026-10-22 10:00:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T10:00:58Z.


Thanks @MarcZX10, for re-checking it against current state. You're right that it's 91 commits behind and was validated on the old base. I'm converting it to a draft under the #5239 hold, since no title run shows the need. Before marking it ready again I'll rebase it onto current main, rerun guest_standard_streams and the libc suite, and check the libc.md hunk against #9342 (which only adds the gets entry next to it).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6338042564 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9596#issuecomment-6338042564). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T10:00:58Z. --- Thanks @MarcZX10, for re-checking it against current state. You're right that it's 91 commits behind and was validated on the old base. I'm converting it to a draft under the #5239 hold, since no title run shows the need. Before marking it ready again I'll rebase it onto current `main`, rerun `guest_standard_streams` and the libc suite, and check the `libc.md` hunk against #9342 (which only adds the `gets` entry next to it).
MarcZX10 commented 2026-10-22 13:22:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T13:22:47Z.

Review: CHANGES_REQUESTED


The fgetws changes remain supported by the cited bundled-libc disassembly, the Windows standard-stream tests, and the reported 26-test libc stream/wide suite. I rechecked exact head d15e72dab340aa2daa0e04221e2f7fb55d6efc75 against current main 3ca07d086d5f44fbe3c00370e7f03953ab46ca1b: whitespace and conventions pass, the current-main merge tree is clean, and both changed C++ translation units pass local GCC 12.2 C++20 syntax-only checks with -Wall -Wextra -Werror (suppressing existing unused/missing-field warnings in FormattingWide.cpp). I did not run the runtime suite locally; hosted Linux/Windows product jobs are skipped.

The branch is based on the earlier 8b3a6ba88d8fc74e768f1a7e8e9e36fb69842768 main, so please update to current main and rerun guest_standard_streams plus the focused libc stream/wide tests, then refresh the Tested head. Keep the #9342 debt-file coordination when rebasing; the reported change adds the separate gets entry adjacent to this one. The disassembly is correctly labeled as bundled-binary evidence, not console measurement.

I withdraw the #5239 intake-hold rationale: if the draft status was only because of that old intake direction, it need not stay draft for that reason. This review request is for current-main validation only; readiness remains your call.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5614898484 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9596#pullrequestreview-5614898484). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T13:22:47Z. **Review:** CHANGES_REQUESTED --- The `fgetws` changes remain supported by the cited bundled-libc disassembly, the Windows standard-stream tests, and the reported 26-test libc stream/wide suite. I rechecked exact head `d15e72dab340aa2daa0e04221e2f7fb55d6efc75` against current main `3ca07d086d5f44fbe3c00370e7f03953ab46ca1b`: whitespace and conventions pass, the current-main merge tree is clean, and both changed C++ translation units pass local GCC 12.2 C++20 syntax-only checks with `-Wall -Wextra -Werror` (suppressing existing unused/missing-field warnings in `FormattingWide.cpp`). I did not run the runtime suite locally; hosted Linux/Windows product jobs are skipped. The branch is based on the earlier `8b3a6ba88d8fc74e768f1a7e8e9e36fb69842768` main, so please update to current main and rerun `guest_standard_streams` plus the focused libc stream/wide tests, then refresh the Tested head. Keep the #9342 debt-file coordination when rebasing; the reported change adds the separate `gets` entry adjacent to this one. The disassembly is correctly labeled as bundled-binary evidence, not console measurement. I withdraw the #5239 intake-hold rationale: if the draft status was only because of that old intake direction, it need not stay draft for that reason. This review request is for current-main validation only; readiness remains your call.
JJenkx commented 2026-10-22 13:28:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T13:28:35Z.


@MarcZX10 rebased onto main b786b907c8 (head 5ada941529). The 26 libc stream, wide and formatting tests, including guest_standard_streams, pass in three runs out of three on Windows. The Tested section is refreshed and records the #9342 gets/fgetws adjacency resolution, with both entries kept verbatim. The PR was a draft only because of the intake hold, so I've marked it ready.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6341955028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9596#issuecomment-6341955028). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T13:28:35Z. --- @MarcZX10 rebased onto `main` b786b907c8 (head 5ada941529). The 26 libc stream, wide and formatting tests, including `guest_standard_streams`, pass in three runs out of three on Windows. The Tested section is refreshed and records the #9342 `gets`/`fgetws` adjacency resolution, with both entries kept verbatim. The PR was a draft only because of the intake hold, so I've marked it ready.
MarcZX10 commented 2026-10-23 11:40:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T11:40:18Z.

Review: APPROVED


Reviewed updated head ea6aa0a809. The focused diff now follows the described bundled libc.prx behavior for sizes below two and for partial reads after fgetwc returns -1, and the tests cover both without broadening the unverified decoding behavior. The refreshed description records 26 focused Windows libc tests passing on three runs, 7 focused Linux tests passing on three runs with a main-vs-PR comparison, conventions passing, and the evidence source accurately labeled as disassembly rather than console measurement. Current hosted checks pass for conventions and the relinker matrix; full Linux/Windows product jobs are skipped by repository policy. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627707873 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9596#pullrequestreview-5627707873). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T11:40:18Z. **Review:** APPROVED --- Reviewed updated head ea6aa0a8091f3ecd4edcb83e1fa0cd6226e84f66. The focused diff now follows the described bundled `libc.prx` behavior for sizes below two and for partial reads after `fgetwc` returns -1, and the tests cover both without broadening the unverified decoding behavior. The refreshed description records 26 focused Windows libc tests passing on three runs, 7 focused Linux tests passing on three runs with a main-vs-PR comparison, conventions passing, and the evidence source accurately labeled as disassembly rather than console measurement. Current hosted checks pass for conventions and the relinker matrix; full Linux/Windows product jobs are skipped by repository policy. No blocking issue found.
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!9596
No description provided.