This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch/ob] fileno: Check for open stream


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]