Repository navigation
test(libc): check fread and fwrite of nothing leave the stream alone - #12481
Conversation
MarcZX10
left a comment
There was a problem hiding this comment.
I reviewed head fe0c8759021e and the new CheckZeroSizedTransfers assertions. They cover both zero size and zero nmemb for reads and writes, preserve the read buffer and stream position, check that the next input still yields the expected byte, and distinguish a zero-element write on a read-only stream from a real invalid write. This matches the C11 stream-state requirements, and I found no defect in the added test.
Hosted Windows, conventions, intake, eligibility and labels checks pass; Linux and unrelated specialized jobs are skipped. The PR reports three focused Windows runs plus a red control. I did not independently run the executable locally in this environment, so this is a scoped comment rather than an approval.
|
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.
|
fe0c875 to
0824780
Compare
rooklz
left a comment
There was a problem hiding this comment.
I ran head 0824780682 on macOS and Linux. The body only covers Windows.
Setup. Each run builds the 67 libc src/*.cpp that compile without submodules, from the head tree, plus tests/FileStreams.cpp, using the target's own -include HostStandardStreams.hpp and -ffp-contract=off. AppArchive.cpp needs zarchive, so I replaced it with a small "no archive mounted" stub. Nothing on the stream path calls it.
- macOS 26.2, Apple clang,
-arch x86_64under Rosetta (arm64 hits the existing long-double static_assert) - Ubuntu 24.04 amd64, GCC 13.3, glibc
Head. file_streams passes on both. main's FileStreams.cpp also passes against the same objects.
Mutants. I edited FileAndHeap.cpp and ran each mutant against the PR test and against main's test:
| mutant | macOS PR / main | Linux PR / main |
|---|---|---|
zero fread returns count |
killed (fread with size 0 must return 0) / survives |
killed / survives |
zero fwrite returns count |
killed (fwrite with size 0 must return 0) / survives |
killed / survives |
fwrite zero check moved after SwitchToWriting (your red control) |
survives / survives | survives / survives |
fread zero check moved after SwitchToReading |
survives / survives | survives / survives |
zero check tests only size, or only count (×2 functions) |
survives / survives | survives / survives |
The survivors are equivalent on POSIX, so the test is not missing anything there. FileStream::SwitchToWriting is a plain return true outside _WIN32. On glibc, SwitchToReading only switches a stream that is currently writing, and this one was flushed and repositioned first. Host fread/fwrite with a zero size or count already return 0 and leave the stream alone. So the read-only fwrite-of-nothing check is a Windows (UCRT flags) regression pin, and the red control in the body can only be reproduced on Windows. That matches the body, and maybe one line in Tested could say so.
Also checked: check_conventions --base upstream/main passes; git merge-tree against upstream b9eafb0c49 is clean; test only, one file. The pr-overlap sharers (#7386, #8150, #11831) all conflict with main already. No defects found.
AI-assisted: yes
|
@rooklz thanks for the macOS and Linux runs and the mutant table. Agreed: outside Windows |
No test passed a zero size or count to fread or fwrite. C11 7.21.8.1 and 7.21.8.2 say both then return 0 and leave the stream unchanged, and fread leaves the array alone. CheckZeroSizedTransfers requires that for size 0 and count 0 on an update stream (buffer, position, end-of-file and error indicators unchanged, the next fgetc reads on), and requires a zero-sized fwrite on a read-only stream not to set the error indicator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0824780 to
fa2f0ef
Compare
What
No test passed a zero size or count to
freadorfwrite. C11 7.21.8.1 and 7.21.8.2: ifsizeornmembis zero,freadreturns zero and the contents of the array and the state of the stream remain unchanged, andfwritereturns zero and the state of the stream remains unchanged.FileStreams.cppgetsCheckZeroSizedTransfers:w+bstream holdingabcdand positioned at 1,freadwith size 0 and with count 0 returns 0, leaves aZZZZ-filled buffer untouched, and leavesftellat 1 with the end-of-file and error indicators clear;fwritewith size 0 and with count 0 returns 0 and leavesftellat 1 and the error indicator clear, and the nextfgetcstill readsb;rb, a zero-sizedfwritereturns 0 without setting the error indicator, unlike a real write to a read-only stream, which the file already checks.Changed:
core/libs/tests/FileStreams.cpp. Test only, no change to the library.Tested
Windows 11 Home 26200, WinLibs GCC 15.2.0 r7, Release, on
main17f8dfad51plus this test.ctest --test-dir build -R '^file_streams$': passes 3 of 3 runs.fwritereturning early for a zero size only after switching the stream to writing,file_streamsaborts onfwrite of nothing on a read-only stream set the error indicator. Restored, it passes again.FileStream::SwitchToWritingis a no-op outside_WIN32, and the hostfread/fwritealready return 0 for a zero size or count. So that check pins the Windows (UCRT) path. rooklz ran the test on macOS and Linux (passes, see the review below).python tools/check_conventions.py --base upstream/main: passed.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