[patch/ob] fileno: Check for open stream
Corinna Vinschen
vinschen@redhat.com
Mon Jul 9 22:16:00 GMT 2012
Hi,
while looking into an error report on the Cygwin list, I came across
the weird fact that fileno always returns a valid descriptor, even if
the stream has been closed. Per SUSv4(*):
The fileno() function may fail if:
[EBADF]
The stream argument is not a valid stream, or the stream is not
associated with a file.
So I just applied a patch which checks if the incoming stream pointer
is open and returns -1 with EBADF if not, see the patch below.
This is not exactly what I'd expect from testing the incoming stream,
but that's a different story I'll write about in my next mail.
Corinna
(*) http://pubs.opengroup.org/onlinepubs/007908799/xsh/fileno.html
Index: libc/stdio/fileno.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fileno.c,v
retrieving revision 1.6
diff -u -p -r1.6 fileno.c
--- libc/stdio/fileno.c 30 May 2012 08:58:42 -0000 1.6
+++ libc/stdio/fileno.c 9 Jul 2012 12:11:13 -0000
@@ -47,6 +47,7 @@ Supporting OS subroutines required: none
#include <_ansi.h>
#include <stdio.h>
+#include <errno.h>
#include "local.h"
int
@@ -56,7 +57,13 @@ _DEFUN(fileno, (f),
int result;
CHECK_INIT (_REENT, f);
_newlib_flockfile_start (f);
- result = __sfileno (f);
+ if (f->_flags)
+ result = __sfileno (f);
+ else
+ {
+ result = -1;
+ _REENT->_errno = EBADF;
+ }
_newlib_flockfile_end (f);
return result;
}
--
Corinna Vinschen
Cygwin Project Co-Leader
Red Hat
More information about the Newlib
mailing list