From: Rich Felker Date: Tue, 28 Aug 2018 22:40:15 +0000 (-0400) Subject: set errno when fileno is called on a FILE with no underlying fd X-Git-Tag: v1.1.20~17 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=b39b47bac8ee5505cfc595000a140c35460e1cac;p=oweals%2Fmusl.git set errno when fileno is called on a FILE with no underlying fd this is a POSIX requirement. also remove the gratuitous locking shenanigans and simply access f->fd under control of the lock. there is no advantage to not doing so, and it made the correctness non-obvious at best. --- diff --git a/src/stdio/fileno.c b/src/stdio/fileno.c index ba7f9391..0bd0e988 100644 --- a/src/stdio/fileno.c +++ b/src/stdio/fileno.c @@ -1,13 +1,16 @@ #include "stdio_impl.h" +#include int fileno(FILE *f) { - /* f->fd never changes, but the lock must be obtained and released - * anyway since this function cannot return while another thread - * holds the lock. */ FLOCK(f); + int fd = f->fd; FUNLOCK(f); - return f->fd; + if (fd < 0) { + errno = EBADF; + return -1; + } + return fd; } weak_alias(fileno, fileno_unlocked);