Nitpicky issue. Currently, FreeBSD's fileno() does not set errno. POSIX issue 6 added an optional EBADF errno, so no problem there. Issue 7 added a mandatory EBADF: > RETURN VALUE > > ... Otherwise, the value -1 shall be returned and errno set to indicate the error. > > ERRORS > > The fileno() function shall fail if: > > [EBADF] > The stream is not associated with a file. https://pubs.opengroup.org/onlinepubs/9799919799/functions/fileno.html (Issue 8 did not alter fileno.) Code example, in case it's helpful: ``` $ cat bad-fileno.c #include <errno.h> #include <stdio.h> int main(void) { FILE * s; int rc; /* Get an invalid stream. */ s = fopen("/dev/null", "r"); fclose(s); /* Set errno to an arbitrary value. */ errno = 123; printf("errno: %i\n", errno); /* errno is still that arbitrary value; rc is correct. */ rc = fileno(s); printf("rc %i, errno: %i\n", rc, errno); } $ clang -Weverything bad-fileno.c && ./a.out errno: 123 rc -1, errno: 123 $ ```
Perhaps: diff --git a/lib/libc/stdio/fileno.c b/lib/libc/stdio/fileno.c index 66502430dc3d..ff317993c0d3 100644 --- a/lib/libc/stdio/fileno.c +++ b/lib/libc/stdio/fileno.c @@ -49,6 +49,9 @@ fileno(FILE *fp) fd = __sfileno(fp); FUNLOCKFILE(fp); + if (fd == -1) + errno = EBADF; + return (fd); }
(In reply to Ed Maste from comment #1) No, fileno() is inlined, which is why this is non-trivial change.
https://reviews.freebsd.org/D47834
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=7cd756ff4fe7e65a9a3f588904998bf6f4b37623 commit 7cd756ff4fe7e65a9a3f588904998bf6f4b37623 Author: Konstantin Belousov <kib@FreeBSD.org> AuthorDate: 2024-11-28 22:12:29 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2024-11-29 15:25:32 +0000 fileno(3): set errno when returning -1 as required by IEEE Std 1003.1™-2024. PR: 283014 Reported by: Graham Percival <gperciva@tarsnap.com> Reviewed by: emaste, imp Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D47834 include/stdio.h | 4 ---- lib/libc/stdio/ferror.3 | 20 ++++++++++++++++++-- lib/libc/stdio/fileno.c | 25 ++++++++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-)
A commit in branch stable/14 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=9fb5c02356b0717aa3cdde3c19adc6d3f0521225 commit 9fb5c02356b0717aa3cdde3c19adc6d3f0521225 Author: Konstantin Belousov <kib@FreeBSD.org> AuthorDate: 2024-11-28 22:12:29 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2024-12-03 00:39:22 +0000 fileno(3): set errno when returning -1 PR: 283014 (cherry picked from commit 7cd756ff4fe7e65a9a3f588904998bf6f4b37623) include/stdio.h | 4 ---- lib/libc/stdio/ferror.3 | 20 ++++++++++++++++++-- lib/libc/stdio/fileno.c | 25 ++++++++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-)