Repository navigation
fix(libc): fail fread with EINVAL when size times count overflows - #8509
Conversation
…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>
|
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.
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.
|
fogydes
left a comment
There was a problem hiding this comment.
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-86andfwrite.c:67-75, returning 0 and setting stream error status rather than terminating with an unhandled C++ exception. - Early exit for
size == 0 || count == 0preceding 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_testson 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.
|
Thanks. Before this goes further I compared it with the console's own
Suggested shape:
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>
|
Thank you for reading the console's
New head Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release,
AI-assisted (Claude Code). |
MarcZX10
left a comment
There was a problem hiding this comment.
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>
|
Thanks, you are right:
AI-assisted (Claude Code). |
MarcZX10
left a comment
There was a problem hiding this comment.
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.
|
Linux validation of current head
Environment: Debian 13.7 x86-64, Linux 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 -VTracked 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. |
|
Checked the diff against the console |
What
freadthrewstd::overflow_errorwhensize * countdoes not fit insize_t. The console'slibc.prx(read by the maintainer from the module of PPSA21564, not run on a console) setserrnotoEINVALand returns 0 before it touches the stream, soferrorstays clear.freadnow does the same, after the existing early return forsize == 0orcount == 0.fwriteis not changed: the console has no overflow check there and multiplies with wraparound, so the existing throw stays.Fixes #8508
git merge-treeagainstupstream/mainis clean.pr_overlapwas 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 + heade5df7a3083:file_streams: fails (what(): fread: buffer size overflow) withFileAndHeap.cpptaken 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 withEINVALandferrorclear; thefwriteoverflow still throws.freadorfwritepass (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_trustwas not run (executable not built in my tree).python tools/check_conventions.py --base upstream/main: passes.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code