From nobody@FreeBSD.org  Fri Jan 19 14:25:54 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 11FFA16A400
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 19 Jan 2007 14:25:54 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.freebsd.org (Postfix) with ESMTP id 03A7413C442
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 19 Jan 2007 14:25:54 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l0JEPrw2072788
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 19 Jan 2007 14:25:53 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l0JEPr1Z072787;
	Fri, 19 Jan 2007 14:25:53 GMT
	(envelope-from nobody)
Message-Id: <200701191425.l0JEPr1Z072787@www.freebsd.org>
Date: Fri, 19 Jan 2007 14:25:53 GMT
From: Trenton Schulz<twschulz@trolltech.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Files cache their eof status
X-Send-Pr-Version: www-3.0

>Number:         108118
>Category:       bin
>Synopsis:       [libc] files should not cache their EOF status
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jan 19 14:30:17 GMT 2007
>Closed-Date:    
>Last-Modified:  Sat Apr  4 15:00:05 UTC 2009
>Originator:     Trenton Schulz
>Release:        FreeBSD 6.0 (reproduced on 6.2-RC as well)
>Organization:
Trolltech ASA
>Environment:
FreeBSD stimpy.troll.no 6.0-RELEASE FreeBSD 6.0-RELEASE #0: Wed Dec  7 09:49:59 CET 2005     root@stimpy.troll.no:/usr/obj/usr/src/sys/SMP  i386
>Description:
When opening one file handle for reading binary and another for writing
binary, one can write a byte, flush the file, and then attempt to read
two bytes, the function will fail to read the one byte.

This does not happen on HP-UX, AIX, or Linux


>How-To-Repeat:
#include <stdio.h>
#include <assert.h>

int main()
{
    FILE *writeFile;
    FILE *readFile;
    char readChar;
    int i;

    writeFile = fopen("/tmp/fooFile", "wb");
    readFile = fopen("/tmp/fooFile", "rb");

    for (i = 0; i < 2; i++) {
        fwrite("a", 1, 1, writeFile);
        fflush(writeFile);
        assert(fread(&readChar, 1, 2, readFile) > 0);
    }
    return 0;
}

>Fix:
The programmer can call fseek() and try again. But it's a bit of second
guessing. I guess it shouldn't cache the eof check.
>Release-Note:
>Audit-Trail:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, twschulz@trolltech.com,
	freebsd-standards@freebsd.org
Cc:  
Subject: Re: bin/108118: [libc] files should not cache their EOF status
Date: Sat, 4 Apr 2009 16:56:29 +0200

 The way I read POSIX, FreeBSD's current behaviour seems correct. Calling
 fread(3) is equivalent to calling fgetc(3) an appropriate number of
 times, and fgetc(3) shall fail if the end-of-file indicator is set for
 the stream, even if data is available on the underlying file.
 
 Apparently, POSIX aligns with the C standard here; System V tradition is
 not to check the end-of-file indicator here. Both
 src/lib/libc/stdio/refill.c (__srefill()) and Solaris fgetc(3) manpage
 agree about this.
 
 -- 
 Jilles Tjoelker
>Unformatted:
