Skip to content

test(libc): check fread and fwrite of nothing leave the stream alone - #12481

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Oscar45397:test/libc-fread-fwrite-zero
Oct 29, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Oscar45397:test/libc-fread-fwrite-zero

Conversation

@Oscar45397

@Oscar45397 Oscar45397 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

What

No test passed a zero size or count to fread or fwrite. C11 7.21.8.1 and 7.21.8.2: if size or nmemb is zero, fread returns zero and the contents of the array and the state of the stream remain unchanged, and fwrite returns zero and the state of the stream remains unchanged. FileStreams.cpp gets CheckZeroSizedTransfers:

  • on a w+b stream holding abcd and positioned at 1, fread with size 0 and with count 0 returns 0, leaves a ZZZZ-filled buffer untouched, and leaves ftell at 1 with the end-of-file and error indicators clear;
  • fwrite with size 0 and with count 0 returns 0 and leaves ftell at 1 and the error indicator clear, and the next fgetc still reads b;
  • on the same file opened rb, a zero-sized fwrite returns 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 main 17f8dfad51 plus this test.

  • ctest --test-dir build -R '^file_streams$': passes 3 of 3 runs.
  • Red control: with fwrite returning early for a zero size only after switching the stream to writing, file_streams aborts on fwrite of nothing on a read-only stream set the error indicator. Restored, it passes again.
  • Off Windows the read-only check and this red control can't fail: FileStream::SwitchToWriting is a no-op outside _WIN32, and the host fread/fwrite already 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

  • 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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with 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.

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.

@github-actions

github-actions Bot commented Oct 28, 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 11831 core/libs/tests/FileStreams.cpp L268-269

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 7386 core/libs/tests/FileStreams.cpp
PR 8150 core/libs/tests/FileStreams.cpp

@Oscar45397
Oscar45397 force-pushed the test/libc-fread-fwrite-zero branch from fe0c875 to 0824780 Compare October 28, 2026 16:20
@oneandonlydean oneandonlydean self-assigned this Oct 28, 2026

@rooklz rooklz 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.

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_64 under 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

@Oscar45397

Copy link
Copy Markdown
Contributor Author

@rooklz thanks for the macOS and Linux runs and the mutant table. Agreed: outside Windows SwitchToWriting is a no-op and the host calls already return 0 for a zero size or count, so the read-only fwrite check pins the UCRT path and the red control only reproduces on Windows. I've added a line to Tested saying so.

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>
@Oscar45397
Oscar45397 force-pushed the test/libc-fread-fwrite-zero branch from 0824780 to fa2f0ef Compare October 28, 2026 21:10
@oneandonlydean
oneandonlydean merged commit 4f2e0fc into boykopovar:main Oct 29, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants