Re: Bug in kernel or libc !?

Brandon S. Allbery KF8NH (allbery@kf8nh.apk.net)
Mon, 24 Aug 1998 18:02:17 -0300


(I had originally replied privately, but I'll tag onto the on-list
discussion instead.)

In message <35E0EE92.5F2FD542@taraz.kz>, Alex Riedel writes:
+-----
| fseek(bank,0L,0); //back to the startfwrite(buf,512,1,bank); //i write data
| to start
| //hier file test is 512 bytes len.
| lseek(fileno(bank),0L,2); //go to the end
| //after lseek file test truncated.
|
| On Solaris without "fflush" this works fine.
| This is not my program. I just ported this prog. from Solaris to Linux.
+--->8

The author clearly ignored the *Solaris* documentation. That it happens to
work on Solaris is an artifact of the stdio implementation it uses; but the
documentation for that implementation tells you that that is not guaranteed
to work in any particular way. So it does not work "fine", it
*accidentally* does what you want. Sun could replace stdio in the next
release of Solaris and cause this buggy program to not accidentally work any
more.

| > You do something bad by improperly using stdio; before you can do anything
| > with fileno() you need to fflush the stream. The Solaris man page for
| > stdio has a reasonable discussion of this.
|
| This is feature? or bug?
+--->8

Neither.

Buffered differs from unbuffered (to state the obvious). Unbuffered
operations *cannot be mixed* with buffered options reliably unless you flush
the buffers (one would think this also would be obvious).

Also note that read and write are not the only things that can be buffered:
one can buffer a seek until it's needed. This is a big win
performance-wise, but causes buggy programs like yours to not accidentally
do what was naively expected of them.

This is the nature of mixing buffered and unbuffered I/O. You *should not*
do this without explicitly flushing buffers before performing unbuffered
operations.

BTW, please go into your web browser's properties and turn off the HTML
trash. This is a mailing list, not a web page, and it's hard enough for me
to wade through the 400 messages in my inbox without having to wait for
messages to load twice, one oversized because of tags and unnecessary
formatting.

-- 
brandon s. allbery	[os/2][linux][solaris][japh]	 allbery@kf8nh.apk.net
system administrator	     [WAY too many hats]	   allbery@ece.cmu.edu
electrical and computer engineering					 KF8NH
carnegie mellon university

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html