fflush() on readonly files

Radim Kolar hsn at netmag.cz
Sat Jun 19 09:58:39 GMT 2004


> > > 3 - Correct some programs depending on this
> > Are there any such programs?
Today I have stepped on another example: advancecomp (in ports):
Code for returning size of file: lib/fz.c
542     /**
543      * Get the size of the file.
544      */
545     long fzsize(adv_fz* f)
546     {
547             if (f->type == fz_file) {
548                     struct stat st;
549                     if (fflush(f->f) != 0) {
550                             return -1;
551                     }

> The behavior of fflush() on a read-only stream is not defined by any
> relevant standards. It is a no-op.
Returning an error is not no-op. It is a failure.

There are 2 cases:
1) Program knows that he has r/o fd and calls fflush() on it.
> In my experience, fflush() is only called on input streams when the Microsoft
> behavior is expected.
2) Program do not knows that he has r/o fd and calls fflush() as part of his normal operation. For example fflush()+fstat() pairs and fflush()+fclose() pairs.
This programs expects that fflush() on r/o files do not fails.

I am interested in solving case #2. I have no troubles with #1 programs; there are not a very common.

> Bottom line: learn C, fix your code.
There are other languages also. Major problem with this behavior lies in
various Python scripts. When they calls fflush() on r/o fd, exception is thrown
and aborts program. They often calls fflush before close in class which
supports both r/o and r/w fd and there is no way to check type of passed
descriptor.


More information about the freebsd-arch mailing list