fflush on closed std stream

Eric Blake ebb9@byu.net
Tue Jul 28 16:37:00 GMT 2009


According to Jeff Johnston on 7/20/2009 10:34 AM:
> This patch appears to fix the issue.  Is it safe to probe fp->_flags
> prior
> to the _flockfile, in order to skip the lock if the stream is not
> currently visiting a file?  Or do I have to approach this in some other
> manner?
> 
>   
>> Probing fp->_flags should be ok.  I do not think we should be setting
>> errno and returning failure.  There is nothing to flush.  Running a test
>> of fclose(stdout); fflush(stdout);  on glibc returns 0 from fflush. 
>> Newlib should do the same.

Also, Solaris set errno to EBADF, but still returned 0.  So here's what I
committed:

> 2009-07-18  Eric Blake  <ebb9@byu.net>
> 
>     Avoid a fault from locking a closed standard file.
>     * libc/stdio/fflush.c (_fflush_r): Give up early if stream has
>     been previously closed.

Index: libc/stdio/fflush.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/fflush.c,v
retrieving revision 1.13
diff -u -p -r1.13 fflush.c
--- libc/stdio/fflush.c	31 Oct 2008 21:08:03 -0000	1.13
+++ libc/stdio/fflush.c	22 Jul 2009 02:16:31 -0000
@@ -93,6 +93,9 @@ _DEFUN(_fflush_r, (ptr, fp),

   CHECK_INIT (ptr, fp);

+  if (!fp->_flags)
+    return 0;
+
   _flockfile (fp);

   t = fp->_flags;

-- 
Don't work too hard, make some time for fun as well!

Eric Blake             ebb9@byu.net

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 319 bytes
Desc: OpenPGP digital signature
URL: <http://sourceware.org/pipermail/newlib/attachments/20090728/50ae77c4/attachment.sig>


More information about the Newlib mailing list