Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 27 Sep 2008 02:30:08 GMT
From:      sean <seburke@rent.com>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: kern/127335: [libc] fwrite(3) fails to generate error when applied to a read-only file
Message-ID:  <200809270230.m8R2U8Nx096700@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR kern/127335; it has been noted by GNATS.

From: sean <seburke@rent.com>
To: bug-followup@FreeBSD.org, rfg@tristatelogic.com
Cc:  
Subject: Re: kern/127335: [libc] fwrite(3) fails to generate error when
	applied to a read-only file
Date: Fri, 26 Sep 2008 18:57:33 -0700

 fwrite does set errno correctly. I modified the demo code
 to call perror before and after, like so:
 
 #include <stdio.h>
 
 int
 main (void)
 {
     perror ("pre-fwrite");
     fwrite ("Hello world!", 1, 12, stdin);
     perror ("post-fwrite");
 
     if (ferror (stdin))
         printf ("Error writing to stdin\n");
     else if (feof (stdin))
         printf ("EOF detected while writing to stdin\n");
     else
         printf ("This shouldn't happen!\n");
 
     return 0;
 }
 
 Which produces this output:
 
 pre-fwrite: Unknown error: 0
 post-fwrite: Bad file descriptor
 This shouldn't happen!
 
 The relevant code is in wsetup.c, where is tests where the
 'WRite' or 'Read-Write' flags are set, and fails if not:
 
         if ((fp->_flags & __SWR) == 0) {
                 if ((fp->_flags & __SRW) == 0) {
                         errno = EBADF;
                         return (EOF);
                 }
 
 I believe that the fix is to set the error flag on failure,
 like so:
 
          if ((fp->_flags & __SWR) == 0) {
                 if ((fp->_flags & __SRW) == 0) {
                         fp->_flags |= __SERR;
                         errno = EBADF;
                         return (EOF);
                 }
  
 
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200809270230.m8R2U8Nx096700>