[PATCH 1/3] tools/nolibc: fix fopen() errno when open() fails

From: Danish Khateeb

Date: Wed Oct 28 2026 - 16:07:59 EST


When open() fails, fopen() passes its -1 on to fdopen(), which sets
errno to EBADF and so overwrites the error from open(). A missing file
is reported as EBADF instead of ENOENT, and a file without permission as
EBADF instead of EACCES.

Return NULL as soon as open() fails, leaving its errno in place.

Fixes: a009a0c6faa9 ("tools/nolibc: add fopen()")
Signed-off-by: Danish Khateeb <danishkhateeb03@xxxxxxxxx>
---
tools/include/nolibc/stdio.h | 3 +++
1 file changed, 3 insertions(+)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index ebdd413d13ec..00a95a267d90 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -79,6 +79,9 @@ FILE *fopen(const char *pathname, const char *mode)
flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;

fd = open(pathname, flags, 0666);
+ if (fd < 0)
+ return NULL;
+
return fdopen(fd, mode);
}

--
2.56.0