fix(libkernel): fail write with EINVAL for a size above SSIZE_MAX #4818

Closed
Gashrod1 wants to merge 1 commit from upstream-pr/4818 into main
Gashrod1 commented 2026-10-12 22:51:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gashrod1 on 2026-10-12T22:51:37Z.


What

write_nid_postfix threw std::invalid_argument when the byte count did not fit in ssize_t, which ends the process. FreeBSD's sys_write returns EINVAL for it before looking up the descriptor (sys/kern/sys_generic.c: if (uap->nbyte > IOSIZE_MAX) return (EINVAL);, where IOSIZE_MAX is SSIZE_MAX with the default debug.iosize_max_clamp=0). write now returns -1 with errno 22 for such a count, for any descriptor.

guest_filesystem checks write with SIZE_MAX on a pipe and with SSIZE_MAX + 1 on descriptor -1 (EINVAL, not EBADF). It fails on main and passes with the change.

Tested

Linux (Arch, GCC), Release, BUILD_TESTING=ON, based on main ee4962190: guest_filesystem aborts on main and passes with the change (3 runs); full build and full ctest pass. Windows (WinLibs MinGW-w64 GCC 15.2.0 r7, NVIDIA RTX 5060), Release, BUILD_TESTING=ON: full build and full ctest pass (1018 tests, the same 7 skipped as on main). No title or homebrew 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 (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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4835923229 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4818). Originally posted by [Gashrod1](https://github.com/Gashrod1) on 2026-10-12T22:51:37Z. --- ### What `write_nid_postfix` threw `std::invalid_argument` when the byte count did not fit in `ssize_t`, which ends the process. FreeBSD's `sys_write` returns EINVAL for it before looking up the descriptor ([`sys/kern/sys_generic.c`](https://github.com/freebsd/freebsd-src/blob/main/sys/kern/sys_generic.c): `if (uap->nbyte > IOSIZE_MAX) return (EINVAL);`, where `IOSIZE_MAX` is `SSIZE_MAX` with the default `debug.iosize_max_clamp=0`). `write` now returns -1 with errno 22 for such a count, for any descriptor. `guest_filesystem` checks `write` with `SIZE_MAX` on a pipe and with `SSIZE_MAX + 1` on descriptor -1 (EINVAL, not EBADF). It fails on `main` and passes with the change. ### Tested Linux (Arch, GCC), Release, `BUILD_TESTING=ON`, based on `main` `ee4962190`: `guest_filesystem` aborts on `main` and passes with the change (3 runs); full build and full `ctest` pass. Windows (WinLibs MinGW-w64 GCC 15.2.0 r7, NVIDIA RTX 5060), Release, `BUILD_TESTING=ON`: full build and full `ctest` pass (1018 tests, the same 7 skipped as on `main`). No title or homebrew run. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
CnRJay commented 2026-10-14 01:14:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:14:16Z.

Review: COMMENTED


The change matches FreeBSD's sys_write. Note that #4929 makes the same size < 0 → EINVAL change in write_nid_postfix (as part of a three-call fix in Stdio.cpp), so the two will conflict on that line. Agreeing with its author which one carries it, and listing it under Depends on, would save a rebase. Your test of SSIZE_MAX + 1 on descriptor -1 (EINVAL before EBADF) is a case #4929 doesn't cover, so it's worth keeping either way.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511933142 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4818#pullrequestreview-5511933142). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T01:14:16Z. **Review:** COMMENTED --- The change matches FreeBSD's `sys_write`. Note that #4929 makes the same `size < 0` → `EINVAL` change in `write_nid_postfix` (as part of a three-call fix in `Stdio.cpp`), so the two will conflict on that line. Agreeing with its author which one carries it, and listing it under `Depends on`, would save a rebase. Your test of `SSIZE_MAX + 1` on descriptor -1 (`EINVAL` before `EBADF`) is a case #4929 doesn't cover, so it's worth keeping either way.
oneandonlydean commented 2026-10-14 02:37:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:37:37Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159642441 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4818#issuecomment-6159642441). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:37:37Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

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