fix(libc): return 0 from zero-sized fread and fwrite before reading the stream #16170
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16170"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-05T19:09:08Z.
What
freadandfwritenow return 0 for a zero-sized transfer before they look at the stream, in the order firmware 13.42 checks.fread:size == 0 || count == 0returns 0 first, then thesize * countoverflow setsEINVALand returns 0, then the stream checks. Before this change a null stream threw (FileStream: null stream) even when nothing was to be read.fwrite: the wrapped productsize * count == 0returns 0 first, then the stream checks. Before, a null stream threw, and a product that wraps to 0 (SIZE_MAX / 2 + 1times 2) threwfwrite: buffer size overflow.core/libs/tests/FileStreams.cpp: the newCheckZeroSizedTransfersOnNullStreamcovers both on a null stream (result 0, buffer anderrnounchanged). The existingfwrite(buffer, SIZE_MAX / 2 + 1, 2, stream)expectation changes from a throw to 0 with the error indicator clear: that product wraps to 0, and the firmware returns 0 for it at0x61ab(below).docs/dev/debt/libc.md: a new entry for the gap left open, anfwritewhose product overflows to a nonzero value, which still throws here.Source.
system/common/lib/libSceLibcInternal.sprxfrom firmware 13.42 (sha256a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285), read from disassembly to eachret, not run, not measured on a console:fread(lbB+UlZqVG0,0x604f0): the zero check comes before anything touches the stream, then the overflow check stores 0x16 through__error(the stub at0x124330jumps through the GOT slot of__error), then0x3830is called with the stream and its fields are read.fwrite(MpxhMh8QFro,0x6190): the product is animulwith no overflow check, and a zero product returns 0 before the stream's field at+0x4is read. The write loop then takes the product as the number of bytes left (0x61e4), which is the gap recorded in the debt entry.What the module does with a null stream once the size is nonzero was not read past
0x3830; the call and the field reads after it take the stream as is, and this PR keeps throwing there.Tested
Windows 11 Pro 10.0.26300, WinLibs GCC 15.2, Release, head
e0523220f3(mainadad8fdfa0merged in):docs/dev/debt/libc.mdonly:mainadded three entries at the place where this branch adds thefwriteentry. Both sides are kept, this branch's entry first; againstmainthe file now differs only by that entry.file_streams,file_position,guest_stream_buffer_host_writeandpublic_headerspass (4 of 4, none Not Run), with their executables andlibcrebuilt.tools/check_conventions.py --base upstream/mainon the committed head: all checks pass. NoAPS5_VABIdeclaration changes.tools/pr_overlap.py --branch fix/libc-zero-size-null-stream-transfers --jobs 8against 1,991 open pull requests: none of the 64 it could compare shares a line or an export. It failed closed on 40 that conflict withmainand sharedocs/dev/debt/libc.md, eight of them alsoFileAndHeap.cppand oneFileStreams.cpp. Their diffs, searched forfreadandfwrite: #7447, #8150, #12682, #12688, #12700 and #12965 only call them in tests, and #18122 adds_nid_disambig1wrappers that call them; none changes either function. #5021's diff is over GitHub's 300-file limit and was not read. The negative control below was not repeated, since the branch's code and test lines are unchanged.Windows 11 Pro 10.0.26300, WinLibs GCC, Release, branch
02e7389bd7onmain04cdd52760(clean merge):file_streams,file_position,guest_stream_buffer_host_writeandpublic_headerspass (4 of 4).core/libs/prx/libc/src/FileAndHeap.cpptaken frommainwith this PR's test kept:file_streamsfails withFileStream: null stream.python3 tools/check_conventions.py --base upstream/mainpasses on the committed head.python3 tools/pr_overlap.py --branch fix/libc-zero-size-null-stream-transfers: the only line overlap is #14610, which also appends to the end ofdocs/dev/debt/libc.md(L335); whichever lands second resolves that one line. No open PR changesfread_nid_postfixorfwrite_nid_postfix: of the open PRs that touchFileAndHeap.cpporFileStreams.cpp, #11831 and #8150 only call them in tests.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-09T08:52:04Z.
Review: COMMENTED
Windows run of head
ecd1a1bd2a, merged ontomain286b14d2f7in one tree with 47 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (x86_64-ucrt-posix-seh, the CI toolchain), Release,BUILD_TESTING=ON.file_streamswith--repeat until-fail:5: 5 / 5 pass.main'sFileAndHeap.cppand this PR's test: 5 / 5 runs fail.