Bug 283014 - POSIX issue 7: fileno should be able to fail with EBADF
Summary: POSIX issue 7: fileno should be able to fail with EBADF
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-11-27 21:11 UTC by Graham Percival
Modified: 2024-12-06 16:06 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Graham Percival 2024-11-27 21:11:50 UTC
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
$
```
Comment 1 Ed Maste freebsd_committer freebsd_triage 2024-11-28 19:27:12 UTC
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);
 }
Comment 2 Konstantin Belousov freebsd_committer freebsd_triage 2024-11-28 19:30:48 UTC
(In reply to Ed Maste from comment #1)
No, fileno() is inlined, which is why this is non-trivial change.
Comment 3 Konstantin Belousov freebsd_committer freebsd_triage 2024-11-28 22:40:21 UTC
https://reviews.freebsd.org/D47834
Comment 4 commit-hook freebsd_committer freebsd_triage 2024-11-29 15:26:28 UTC
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(-)
Comment 5 commit-hook freebsd_committer freebsd_triage 2024-12-03 00:52:16 UTC
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(-)