Re: [PATCH 1/3] tools/nolibc: fix fopen() errno when open() fails
From: Thomas Weißschuh
Date: Thu Oct 29 2026 - 13:39:14 EST
On 2026-10-28 15:07:41-0500, Danish Khateeb wrote:
> 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>
Thanks!
Reviewed-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
> ---
> 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);
> }