[PATCH 0/3] tools/nolibc: fix fopen() errno and "rb+" modes

From: Danish Khateeb

Date: Wed Oct 28 2026 - 17:25:39 EST


fopen() has two bugs:

- When open() fails, fopen() overwrites its errno with EBADF, so a
missing file is reported as EBADF instead of ENOENT.

- "rb+", "wb+" and "ab+" are not recognised as update modes, so the
stream is opened read-only or write-only.

Patches 1 and 2 fix them. Patch 3 adds nolibc-test cases for both.

Tested on top of nolibc/for-next 700ce4da6f6f, on x86_64 (GCC and
clang) and i386, and on arm, arm64 and sparc64 under qemu-user:

- nolibc-test, all tests: no failures with the series. The only
difference from before is the three new tests, which pass. They also
pass against glibc (make libc-test).

- Without patches 1 and 2, all three new tests fail on every build.

- On x86_64, a separate program opening a file with each of the twelve
C mode strings now gets the same access mode from nolibc as from
glibc, and the same errno for ENOENT and EACCES failures.

An LLM was used to help with this series.

Danish Khateeb (3):
tools/nolibc: fix fopen() errno when open() fails
tools/nolibc: fix fopen() with the "rb+", "wb+" and "ab+" modes
selftests/nolibc: test fopen() errors and update modes

tools/include/nolibc/stdio.h | 5 +++-
tools/testing/selftests/nolibc/nolibc-test.c | 24 ++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)


base-commit: 700ce4da6f6f172435349b687d2d916e791c6330
--
2.56.0