freopen/fread/popen bug

Achim Gratz
Fri Feb 27 23:51:00 GMT 2015

Eric Blake writes:
>> What's really happening is that we need to peek at the first few bytes
>> of f before deciding which program to call in popen.  After peeking,
>> there's a call to fseek(f,0,0) before the popen, with the intention that
>> the child receives a file pointer set to the beginning of the file.  (I
>> left this out of my STC because it didn't affect the outcome.)  This
>> apparently works on Linux.
> Corinna is correct - you need to fflush() after the fseek() for it to
> affect the underlying offset.  Or, just use read()/lseek() instead of
> fread()/fseek() in the parent, to avoid stdio buffering altogether.
> That way, you'll guarantee the offset the child process will inherit
> without having to worry about flushing buffered state.

Basically this is the same category of bug that was in rcs for a while.
It isn't easy to find out what is and isn't guaranteed about these
things across a fork and since things work slightly differently on Linux
that's something you can probably expect to show up more often.

+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

Samples for the Waldorf Blofeld:

Problem reports:
Unsubscribe info:

More information about the Cygwin mailing list