[Bug 283014] POSIX issue 7: fileno should be able to fail with EBADF

From: <bugzilla-noreply_at_freebsd.org>
Date: Wed, 27 Nov 2024 21:11:50 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=283014

            Bug ID: 283014
           Summary: POSIX issue 7: fileno should be able to fail with
                    EBADF
           Product: Base System
           Version: CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: standards
          Assignee: standards@FreeBSD.org
          Reporter: gperciva@tarsnap.com

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
$
```

-- 
You are receiving this mail because:
You are the assignee for the bug.