ucrt: fgets with room for only the NUL returns an empty string #63

Manually merged
github merged 2 commits from gh-pr/63/head into gh-pr/63/base 2026-11-26 14:44:51 +00:00
Owner

@⁠Repla09 on GitHub · 2026-11-26 12:13 UTC · original
Repla09:fix/ucrt-fgets-one-byte-buffer → main · +28 −3 · 1 files · 2 commits · merged 2026-11-26 12:48 UTC

fgets(buf, 1, f) has room only for the terminator, so it reads nothing. Since no end of file was met, C (7.21.7.2) has it store "" and return buf; the host libc does the same (a C check on macOS prints ret=buf b0=0 next=a). The host returned NULL whenever no character was read.

Now it returns NULL only when an end of file or error ends the read before any character.

fgets_with_room_for_only_the_nul_stores_an_empty_string fails on main. It checks that n = 1 stores "", returns the buffer, consumes nothing and sets no end of file, and that an end of file before any character still returns NULL.

Testing

  • cargo test --target x86_64-apple-darwin -p victory-hle passes (222 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter here).
  • Clippy on x86_64-apple-darwin shows no new warnings; rustfmt --check and cargo xtask policy pass.
**@⁠Repla09** on GitHub · 2026-11-26 12:13 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/63) `Repla09:fix/ucrt-fgets-one-byte-buffer` → `main` · +28 −3 · 1 files · 2 commits · merged 2026-11-26 12:48 UTC `fgets(buf, 1, f)` has room only for the terminator, so it reads nothing. Since no end of file was met, C (7.21.7.2) has it store `""` and return `buf`; the host libc does the same (a C check on macOS prints `ret=buf b0=0 next=a`). The host returned NULL whenever no character was read. Now it returns NULL only when an end of file or error ends the read before any character. `fgets_with_room_for_only_the_nul_stores_an_empty_string` fails on main. It checks that `n = 1` stores `""`, returns the buffer, consumes nothing and sets no end of file, and that an end of file before any character still returns NULL. ## Testing - `cargo test --target x86_64-apple-darwin -p victory-hle` passes (222 unit, 15 native_probe; the D3D12 probe test skipped, no Metal Shader Converter here). - Clippy on `x86_64-apple-darwin` shows no new warnings; `rustfmt --check` and `cargo xtask policy` pass.
fgets(buf, 1, f) reads nothing and, since no end of file was met,
stores "" and returns buf (C 7.21.7.2; the host libc does the same).
The host returned NULL whenever no character was read. Return NULL
only when an end of file or error ends the read before any character.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:44:49 +00:00
github manually merged commit 1452b44de1 into gh-pr/63/base 2026-11-26 14:44:51 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:45 UTC · original · approved these changes

Looks good: n<=0 still gives EINVAL and NULL, n==1 stores an empty string without reading, and EOF/error paths are unchanged, matching C 7.21.7.2 and UCRT. The test fails on main. Approved.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:45 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/63#pullrequestreview-6074348085) · **approved these changes** Looks good: n<=0 still gives EINVAL and NULL, n==1 stores an empty string without reading, and EOF/error paths are unchanged, matching C 7.21.7.2 and UCRT. The test fails on main. Approved.
This discussion has been locked. Commenting is limited to contributors.
No description provided.