From hsn@netmag.cz  Sat Apr 10 10:19:00 2004
Return-Path: <hsn@netmag.cz>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9A31D16A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 10 Apr 2004 10:19:00 -0700 (PDT)
Received: from mail.tiscali.cz (stateless1.tiscali.cz [213.235.135.70])
	by mx1.FreeBSD.org (Postfix) with ESMTP id CBBB843D4C
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 10 Apr 2004 10:18:59 -0700 (PDT)
	(envelope-from hsn@netmag.cz)
Received: from asura.bsd (213.235.70.71) by mail.tiscali.cz (6.7.021)
        id 40292FFD00F3B06C for FreeBSD-gnats-submit@freebsd.org; Sat, 10 Apr 2004 19:18:58 +0200
Received: from hsn@localhost
	by asura.bsd (Exim 4.30_2 FreeBSD) id 1BCKYi-0000X1-RK
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 10 Apr 2004 17:38:16 +0200
Message-Id: <E1BCKYi-0000X1-RK@asura.bsd>
Date: Sat, 10 Apr 2004 17:38:16 +0200
From: Radim Kolar <hsn@netmag.cz>
Reply-To: Radim Kolar <hsn@netmag.cz>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: patch: fflush() should not return error on readonly files
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         65402
>Category:       kern
>Synopsis:       patch: fflush() should not return error on readonly files
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    cperciva
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Apr 10 10:20:19 PDT 2004
>Closed-Date:    Sun Jul 04 20:20:26 GMT 2004
>Last-Modified:  Sun Jul 04 20:20:26 GMT 2004
>Originator:     Radim Kolar
>Release:        FreeBSD 5.2.1-RELEASE-p3 i386
>Organization:
Sanatana Dharma
>Environment:
System: FreeBSD asura.bsd 5.2.1-RELEASE-p3 FreeBSD 5.2.1-RELEASE-p3 #6: Mon Apr 5 17:41:24 CEST 2004 root@asura.bsd:/usr/src/sys/i386/compile/UP i386
>Description:
When fflush() function is called on file which is opened for read-only,
the call fails with bad file descriptor error. This behaviour is different 
from fsync() kernel function which correctly do not return any error.

I have checked different operation systems (linux, cygwin, msvc++, hpux) and
everywhere fflush() on r/o files works correctly. I have found this problem
while trying to port some application to freebsd, fixing the application is
difficult, since it uses the same set classes for read and write access to
datafiles.
There is no reason for returning error to user. If there are no data to write,
simply do nothing. This patch do not breaks any existing application.

>How-To-Repeat:
#include <stdio.h>
int main (int argc,const char *argv[])
{
        FILE *f;
	    char buf[2222];
	    f=fopen("/etc/passwd","r");
	    fread(buf, 10, 1, f);
	    printf("ff rc %d\n",fflush(f)); /* check errno here */
	    fclose(f);
}
>Fix:
--- /usr/src/lib/libc/stdio/fflush.c	Fri Mar 22 22:53:04 2002
+++ fflush.c	Sat Apr 10 17:22:01 2004
@@ -62,8 +62,7 @@
 		return (_fwalk(sflush_locked));
 	FLOCKFILE(fp);
 	if ((fp->_flags & (__SWR | __SRW)) == 0) {
-		errno = EBADF;
-		retval = EOF;
+		return (0);
 	} else
 		retval = __sflush(fp);
 	FUNLOCKFILE(fp);
@@ -82,8 +81,7 @@
 	if (fp == NULL)
 		return (_fwalk(sflush_locked));
 	if ((fp->_flags & (__SWR | __SRW)) == 0) {
-		errno = EBADF;
-		retval = EOF;
+		return (0);
 	} else
 		retval = __sflush(fp);
 	return (retval);

>Release-Note:
>Audit-Trail:

From: Colin Percival <colin.percival@wadham.ox.ac.uk>
To: freebsd-gnats-submit@FreeBSD.org, hsn@netmag.cz
Cc:  
Subject: Re: kern/65402: patch: fflush() should not return error on
  readonly files
Date: Thu, 15 Apr 2004 13:01:43 +0100

   According to the ISO C standard, the result of calling
 fflush() on anything other than an output or update stream
 is undefined.  FreeBSD's behaviour here is consequently
 correct; any behaviour would be correct.
   Programs which rely upon fflush() not returning an error
 when passed a file which is opened read-only are broken,
 and should be fixed.
 
 Colin Percival
 

From: Radim Kolar <hsn@netmag.cz>
To: Colin Percival <colin.percival@wadham.ox.ac.uk>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: kern/65402: patch: fflush() should not return error on readonly files
Date: Fri, 16 Apr 2004 18:47:32 +0200

 > FreeBSD's behavior here is consequently correct; any behaviour would be
 > correct.  Programs which rely upon fflush() not returning an error when
 > passed a file which is opened read-only are broken, and should be fixed.
 I agree with you that my problem application rely upon nonportable behaviour.
 This is not reason for not including this patch.
 
 My points for including this patch are:
 
 1) do not breaks ISO standard
 2) do not breaks existing apps
 3) improve FreeBSD source code compatibility with other OSes, because these OSes are doing the same.
 4) fixes problem with my application, which is large (10MB) and difficult to fix.
 
 In summary: This patch will make things better, not worse. I do not know
 about negative side-effects.
 
 Radim
State-Changed-From-To: open->closed 
State-Changed-By: cperciva 
State-Changed-When: Sun Jul 4 20:18:48 GMT 2004 
State-Changed-Why:  
As (now) documented in src/lib/libc/stdio/fflush.c, this behaviour is 
intentional and will not be changed. 



Responsible-Changed-From-To: freebsd-bugs->cperciva 
Responsible-Changed-By: cperciva 
Responsible-Changed-When: Sun Jul 4 20:18:48 GMT 2004 
Responsible-Changed-Why:  
As (now) documented in src/lib/libc/stdio/fflush.c, this behaviour is 
intentional and will not be changed. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=65402 
>Unformatted:
