delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/06/14/01:29:20

X-Spam-Check-By: sourceware.org
Message-ID: <448F9EA1.7050908@tlinx.org>
Date: Tue, 13 Jun 2006 22:29:05 -0700
From: Linda Walsh <cygwin AT tlinx DOT org>
User-Agent: Thunderbird 1.5.0.4 (Windows/20060516)
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Cygwin fread on Write-Only File Descriptor returns undefined state
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

I think I've run into a problem involving fread.
I open a test file (fopen) with Write-Only access ("w"),
immediately after that, I do an fread on the file handle
of some number of bytes. 

I expect fread to return an error.  But it doesn't. 
It returns 0 as the number of bytes read, and the
error value remains unset when querying with ferror.

In addition to fread not setting the error value, a value
of zero is returned.  Zero is to be returned, *only* on
end-of-file or error.  However, in the test case, neither
value is returned.  Theoretically, this shouldn't happen
(i.e. it's an undefined "set" of return values).

test program follows:
-------------------
#include <stdio.h>
main (int argc, char * argv[]) {

    FILE * output_handle=fopen("/tmp/tmpfile","w");
    if (!output_handle) {
        printf ("can't open output file /tmp/tmpfile\n");
        exit (1);
    }  

    char mybuff[4];
    int retval=fread(mybuff, sizeof(char), 4, output_handle);
    if (!retval) {
        int eof=feof(output_handle);
        int err=ferror(output_handle);
        printf ("(retval,eof,err) = (%d,%d,%d)\n",retval,eof,err);
        if (!eof && ! err) 
            printf ("Undefined error: fread returned zero, but there is 
no eof or error\n");
        exit(2);
    } else
        printf ("Unexpected success in fread: Read %d chars\n",retval);
   
    exit(0);
}
   

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019