This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Cygwin fread on Write-Only File Descriptor returns undefined state


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]