fix(libc): fail fgets with EINVAL and the error indicator for a size of 0 or less #2711

Closed
husseinkad wants to merge 2 commits from upstream-pr/2711 into main
husseinkad commented 2026-10-10 11:53:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T11:53:48Z.


What

The guest fgets handed the size straight to the host. On Windows a size of 0 returned NULL with errno untouched and no stream error indicator, and a negative size got a Windows-runtime errno but no indicator. FreeBSD's lib/libc/stdio/fgets.c (releng/11.4 and main) handles n <= 0 before reading anything: it sets __SERR on the stream, sets errno = EINVAL and returns NULL, leaving the buffer and the file position untouched; fgetws is defined the same way.

fgets_nid_postfix now does the same for size <= 0: SetEncodingError() (what ferror and clearerr read, sticky until clearerr, like __SERR), errno = 22, NULL, buffer and position untouched. Sizes of 1 and more go to the host exactly as before. The repo has only fgets among the fgets-like exports, so there is no sibling to change.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release:

  • guest_standard_streams gains sizes 0, -1 and INT32_MIN (return NULL, errno 22, buffer unchanged, ftello 0, ferror set and cleared by clearerr) and size 1 as a control (writes the NUL, reads nothing). It fails without the change (exit 0xc0000409) and passes with it; ctest -R "guest_standard_streams|file_streams" passes.
  • Outside the repo, main's and this branch's fgets side by side: 336 combinations (8 sizes from INT_MIN to INT_MAX x 21 stream scenarios: regular, empty, at EOF, write-only, closed, redirected stdin in text and binary mode, pending ungetc, stream already in error, CRLF, embedded NUL, content longer than the buffer or exactly size-1 bytes x two errno presets): the 210 combinations with size 1 or more are byte-identical (return value, buffer, position, errno, flags, follow-up reads); the 116 differing combinations of size 0 or less are exactly the intended change.
  • tools/check_conventions.py --base origin/main passes; tools/pr_overlap.py --branch reports no overlap.
  • Linux not run.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4810997290 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2711). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T11:53:48Z. --- ### What The guest `fgets` handed the size straight to the host. On Windows a size of 0 returned NULL with `errno` untouched and no stream error indicator, and a negative size got a Windows-runtime `errno` but no indicator. FreeBSD's `lib/libc/stdio/fgets.c` (releng/11.4 and `main`) handles `n <= 0` before reading anything: it sets `__SERR` on the stream, sets `errno = EINVAL` and returns NULL, leaving the buffer and the file position untouched; `fgetws` is defined the same way. `fgets_nid_postfix` now does the same for `size <= 0`: `SetEncodingError()` (what `ferror` and `clearerr` read, sticky until `clearerr`, like `__SERR`), `errno = 22`, NULL, buffer and position untouched. Sizes of 1 and more go to the host exactly as before. The repo has only `fgets` among the fgets-like exports, so there is no sibling to change. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release: - `guest_standard_streams` gains sizes 0, -1 and INT32_MIN (return NULL, `errno` 22, buffer unchanged, `ftello` 0, `ferror` set and cleared by `clearerr`) and size 1 as a control (writes the NUL, reads nothing). It fails without the change (exit `0xc0000409`) and passes with it; `ctest -R "guest_standard_streams|file_streams"` passes. - Outside the repo, `main`'s and this branch's `fgets` side by side: 336 combinations (8 sizes from INT_MIN to INT_MAX x 21 stream scenarios: regular, empty, at EOF, write-only, closed, redirected stdin in text and binary mode, pending `ungetc`, stream already in error, CRLF, embedded NUL, content longer than the buffer or exactly size-1 bytes x two `errno` presets): the 210 combinations with size 1 or more are byte-identical (return value, buffer, position, `errno`, flags, follow-up reads); the 116 differing combinations of size 0 or less are exactly the intended change. - `tools/check_conventions.py --base origin/main` passes; `tools/pr_overlap.py --branch` reports no overlap. - Linux not run. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [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
github-actions[bot] commented 2026-10-10 11:53:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T11:53:58Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097218300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2711#issuecomment-6097218300). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T11:53:58Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

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!2711
No description provided.