From owner-freebsd-bugs@FreeBSD.ORG Sat Sep 27 02:30:09 2008 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 353511065699 for ; Sat, 27 Sep 2008 02:30:09 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 237238FC16 for ; Sat, 27 Sep 2008 02:30:09 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.2/8.14.2) with ESMTP id m8R2U82p096703 for ; Sat, 27 Sep 2008 02:30:08 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.2/8.14.1/Submit) id m8R2U8Nx096700; Sat, 27 Sep 2008 02:30:08 GMT (envelope-from gnats) Date: Sat, 27 Sep 2008 02:30:08 GMT Message-Id: <200809270230.m8R2U8Nx096700@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org From: sean Cc: Subject: Re: kern/127335: [libc] fwrite(3) fails to generate error when applied to a read-only file X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: sean List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Sep 2008 02:30:09 -0000 The following reply was made to PR kern/127335; it has been noted by GNATS. From: sean 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 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); }