Skip to content

fix(libc): fail fread with EINVAL when size times count overflows - #8509

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/stdio-transfer-overflow
Oct 22, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/stdio-transfer-overflow

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What

fread threw std::overflow_error when size * count does not fit in size_t. The console's libc.prx (read by the maintainer from the module of PPSA21564, not run on a console) sets errno to EINVAL and returns 0 before it touches the stream, so ferror stays clear. fread now does the same, after the existing early return for size == 0 or count == 0. fwrite is not changed: the console has no overflow check there and multiplies with wraparound, so the existing throw stays.

Fixes #8508

git merge-tree against upstream/main is clean. pr_overlap was not run. An earlier attempt, #4830, was closed with "no title is known"; this version follows the maintainer's reading of the console libc.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, -j4, on upstream/main + head e5df7a3083:

  • file_streams: fails (what(): fread: buffer size overflow) with FileAndHeap.cpp taken back to main (new test kept), passes with the change, 3 of 3 runs. New cases: fread(buf, 2, SIZE_MAX / 2 + 1, fp) returns 0 with EINVAL and ferror clear; the fwrite overflow still throws.
  • The other tests that call fread or fwrite pass (12 of 12): guest_binary_word, guest_fopen_long_host_path, guest_path_case, guest_standard_streams (3 variants), guest_savedata_delete, guest_filesystem, guest_host_standard_error_windows, guest_sandbox_certificates, guest_append_streams. guest_sandbox_certificate_trust was not run (executable not built in my tree).
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not run: Linux, a console, a title 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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…erflows

FreeBSD 11.4 fread and fwrite return 0 with errno EINVAL and the error
flag set when size * count does not fit size_t (lib/libc/stdio/fread.c:78-86,
fwrite.c:67-75). The guest threw std::overflow_error, which ends the title.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 8982 core/libs/tests/FileStreams.cpp L5

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/tests/FileStreams.cpp
PR 7081 core/libs/prx/libc/src/FileAndHeap.cpp
PR 7201 core/libs/prx/libc/src/FileAndHeap.cpp
PR 7447 core/libs/prx/libc/src/FileAndHeap.cpp
PR 8150 core/libs/tests/FileStreams.cpp

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 20, 2026

@fogydes fogydes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff, verified FreeBSD 11.4 behavior, and built/ran the test suite locally.

Implementation: Replaces the throw std::overflow_error in fread_nid_postfix and fwrite_nid_postfix when count > max / size with errno = 22; stream->SetError(); return 0;.

  • Matches FreeBSD 11.4 lib/libc/stdio/fread.c:78-86 and fwrite.c:67-75, returning 0 and setting stream error status rather than terminating with an unhandled C++ exception.
  • Early exit for size == 0 || count == 0 preceding this check remains intact and returns 0 without setting error.

Tests: Adds test coverage in core/libs/tests/FileStreams.cpp verifying both fwrite and fread with SIZE_MAX / 2 + 1 and 2 return 0, set errno == EINVAL, set stream error (ferror() != 0), and verify clearerr() resets the error flag.

Local verification:

  • Built file_streams_tests on Windows 11 with MinGW-w64 GCC 15.2.0 Release.
  • Ran ctest --test-dir build-libs -R "^file_streams$" -V: Passed (0.13s).
  • CI checks pass cleanly across all targets.

No issues found. Solid improvement bringing stream error handling in line with FreeBSD.

@github-actions github-actions Bot added the libc label Oct 20, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks. Before this goes further I compared it with the console's own libc.prx, read from the module that a retail title (PPSA21564) ships. The console's stdio isn't necessarily FreeBSD's (see #3982, where snprintf turned out to be Dinkumware's), and here it differs from FreeBSD in two ways:

  • fread: when size * count overflows, it sets errno to EINVAL and returns 0, as this PR does, but it does not set the stream's error flag. It returns before it touches the stream at all, so ferror stays clear.
  • fwrite: has no overflow check. It multiplies size by count with wraparound and returns 0 only when the wrapped product is 0. With your test's fwrite(buf, SIZE_MAX/2+1, 2, fp), the product wraps to 0, so the console returns 0 with errno unchanged and no error flag. Other overflowing pairs would write the wrapped number of bytes.

Suggested shape:

  • fread: EINVAL, return 0, and leave the error flag alone.
  • fwrite: keep the existing throw. Matching the console would mean writing the wrapped count, and nothing needs that yet.

This comes from reading the module, not from running on a console.

…the fwrite throw

The console's libc.prx (read from PPSA21564 by the maintainer) returns from
fread with EINVAL before it touches the stream, so ferror stays clear, and
its fwrite has no overflow check at all. fread no longer sets the stream
error flag, fwrite keeps throwing, and the test follows.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx xXPBSXx changed the title fix(libc): fail fread and fwrite with EINVAL when size times count overflows fix(libc): fail fread with EINVAL when size times count overflows Oct 20, 2026
@xXPBSXx

xXPBSXx commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for reading the console's libc.prx; I changed the PR to match both points.

  • fread: still EINVAL and return 0 when size * count overflows, and it no longer sets the stream's error flag (ferror stays 0).
  • fwrite: back to the existing std::overflow_error; the overflow case is no longer part of this PR.
  • Test (file_streams): fread(buf, 2, SIZE_MAX / 2 + 1, fp) returns 0 with EINVAL and ferror clear, and the fwrite overflow still throws.

New head e5df7a3083 (a follow-up commit on top, no amend). The commit message of the first commit still says "fread and fwrite"; I updated the PR title, the description and the linked issue to "fread" only, with your reading of the console's libc.prx as the source instead of FreeBSD.

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, -j4, on upstream/main + this head:

  • file_streams: fails with what(): fread: buffer size overflow with FileAndHeap.cpp taken back to main (new test kept), passes with the change, 3 of 3 runs.
  • The other tests that call fread or fwrite pass: guest_binary_word, guest_fopen_long_host_path, guest_path_case, guest_standard_streams (3 variants), guest_savedata_delete, guest_filesystem, guest_host_standard_error_windows, guest_sandbox_certificates, guest_append_streams (12 of 12). python tools/check_conventions.py --base upstream/main passes.
  • Not run: Linux, a console.

AI-assisted (Claude Code).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One behavior detail still differs from the measured console contract. fread_nid_postfix checks IsOpen() and calls GetStreamHandle(stream) before the multiplication-overflow branch; a null stream throws, and a closed stream returns EBADF before the measured EINVAL. The maintainer’s libc read says the overflow path returns before touching the stream. Please move the overflow check ahead of both stream checks/handle acquisition (while preserving the zero-size/count behavior), and add a regression showing an overflowing request with a null stream returns 0 with EINVAL rather than throwing. Use EINVAL rather than the literal 22 as well.

The console's fread returns EINVAL and 0 for an overflowing size * count
before it reads the stream, so a null or closed stream with an overflowing
request answers EINVAL instead of throwing or EBADF. Move the check ahead
of the open check and the handle lookup, keeping the zero size and zero
count result, and use EINVAL instead of the literal 22.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, you are right: fread looked at the stream first. I moved the overflow check to the top of fread_nid_postfix, ahead of the IsOpen() check and GetStreamHandle. It still only applies when size != 0, so a zero size or zero count keeps returning 0 after the stream checks as before, and it now uses EINVAL instead of 22. The null buffer check also comes after it now, so an overflowing request returns before anything else is looked at.

file_streams gets one more case: fread(buffer, 2, SIZE_MAX / 2 + 1, nullptr) returns 0 with EINVAL. Without the change it throws (std::runtime_error, file_streams aborts); with it the test passes (new head 3a78b97, Windows 10, MinGW GCC 15.2, Release). The 11 tests that call fread/getw pass: file_streams, guest_append_streams, guest_binary_word, guest_filesystem, guest_fopen_long_host_path, guest_path_case, guest_sandbox_certificates, guest_savedata_delete, guest_standard_streams (3 variants). check_conventions passes. Not tested: a closed stream with an overflowing request (same code path, no new case), Linux, macOS, a console.

AI-assisted (Claude Code).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current head resolves my requested ordering change: the multiplication overflow check runs before IsOpen() and GetStreamHandle, and the new null-stream regression verifies it returns 0 with EINVAL instead of throwing. The zero-size/count guard remains safe from division by zero because the overflow condition is guarded by size != 0. I reviewed the current diff; required checks pass across Linux, Windows, macOS, and ASan. Approved.

@Filip722

Copy link
Copy Markdown
Contributor

Linux validation of current head 3a78b97f5169b4b477380dc478be4d207045ee04, including the follow-up that checks overflow before accessing the stream:

  • Ten registered cases each passed three consecutive executions (30 successful executions, no failures or skips): file_streams, guest_append_streams, guest_binary_word, guest_filesystem, guest_path_case, guest_sandbox_certificates, guest_savedata_delete, and all three guest_standard_streams variants.
  • The unmodified regression confirms overflowing fread on an open stream returns 0 with EINVAL and leaves ferror clear; the null-stream overflow case also returns 0 with EINVAL; overflowing fwrite still throws.
  • I additionally tested the closed-stream case mentioned as untested in the latest follow-up. A temporary copy of the existing FileStreams.cpp, linked to this PR's libc, adds three assertions immediately after the existing FileStream closed(...); closed.Close();: overflow returns 0/EINVAL, and zero-size/zero-count calls each return 0/EBADF. This extended test passed three executions. No tracked test changes were made.
  • Negative control: kept the PR regression, replaced only FileAndHeap.cpp with merge-base d28cb2a66e4deea1a08990b78c4097946a5c1bd0 and rebuilt libc. file_streams fails with std::overflow_error: fread: buffer size overflow. Restoring the exact PR source and rebuilding makes it pass again.

Environment: Debian 13.7 x86-64, Linux 7.2.9-x64v3-xanmod1, AMD Ryzen 5 7640HS, GCC 15.2.0, CMake 3.31.6. Full Release configuration, BUILD_TESTING=ON, ANYPS5_RELINKER_ONLY=OFF, bundled dependencies; built the affected libc, selected test targets and dependencies with two jobs.

ctest --test-dir build \
  -R '^(file_streams|guest_append_streams|guest_binary_word|guest_filesystem|guest_path_case|guest_sandbox_certificates|guest_savedata_delete|guest_standard_streams(_closed_stdout|_exclusive_stdout)?)$' \
  --repeat until-fail:3 -V

Tracked source restored to the named head. No new Windows, macOS, PS5 hardware, title, GPU, or full repository test-suite run. Windows-only long-host-path and certificate-trust cases were not registered on Linux.

AI-assisted testing and report.

@adriadam10 adriadam10 self-assigned this Oct 21, 2026
@adriadam10
adriadam10 merged commit 11f976b into boykopovar:main Oct 22, 2026
17 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff against the console libc.prx behaviour described above (EINVAL, return 0, stream untouched, ferror clear, fwrite unchanged), the size != 0 guard against division by zero, and that the new file_streams cases (open stream and null stream) fail on main with the exception. The Windows guest_raise_in_system_call timeout does not touch fread/fwrite and passed on rerun; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix error Closes an issue (Fixes #N) libc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libc: fread throws on size * count overflow where the console returns 0 with EINVAL

6 participants