Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bug in kernel or libc !?

3 views
Skip to first unread message

Alex Riedel

unread,
Aug 24, 1998, 3:00:00 AM8/24/98
to
This is a multi-part message in MIME format.
--------------D697EE4F6C1ED519EEEBA61F
Content-Type: multipart/alternative; boundary="------------153D7D566EE92E2982905088"


--------------153D7D566EE92E2982905088
Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 7bit

Hello!

Please test this program.

After executing this program file 'test' is 0 bytes lenght.

On Solaris 2.5 x86 this works fine.

--
Alex Riedel

--------------153D7D566EE92E2982905088
Content-Type: text/html; charset=koi8-r
Content-Transfer-Encoding: 7bit

<HTML>
Hello!
<PRE>Please test this program.</PRE>

<PRE>After executing this program file 'test' is 0 bytes lenght.</PRE>

<PRE>On Solaris 2.5 x86 this works fine.</PRE>

<PRE>--&nbsp;
Alex Riedel</PRE>
&nbsp;</HTML>

--------------153D7D566EE92E2982905088--

--------------D697EE4F6C1ED519EEEBA61F
Content-Type: text/plain; charset=koi8-r; name="testcopy.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="testcopy.c"

#include <stdio.h>

int main (void)
{
char buf[512];
FILE *bank;
FILE *bank1;

bank1 = fopen("test","w");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fprintf(bank1,"\n BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG");
fclose(bank1);

bank = fopen("test","r+");

fseek(bank,0L,0);
fread(buf,512,1,bank);
fseek(bank,0L,0);

//fflush(bank); //uncoment this for bugfix

ftruncate( fileno(bank), 0 );

fseek(bank,0L,0);
fwrite(buf,512,1,bank);

//fflush(bank); //uncoment this for bugfix

lseek(fileno(bank),0L,2);
}

--------------D697EE4F6C1ED519EEEBA61F--


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

Marc Slemko

unread,
Aug 24, 1998, 3:00:00 AM8/24/98
to
Why do you think there is any bug?

[...]

bank = fopen("test","r+");

You open a file using stdio.

fseek(bank,0L,0);
fread(buf,512,1,bank);
fseek(bank,0L,0);

You read the data from it then to back to the start.



//fflush(bank); //uncoment this for bugfix

ftruncate( fileno(bank), 0 );

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.

Any behaviour now is undefined because you are mixing stdio and non-stdio
on the same descriptor without proper caution.

Alex Riedel

unread,
Aug 24, 1998, 3:00:00 AM8/24/98
to

--------------C0E1DEFC3CAA3EE0A83DC3F8

Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 7bit

Marc Slemko wrote:

> Why do you think there is any bug?
>
> [...]
> bank = fopen("test","r+");
>
> You open a file using stdio.
>
> fseek(bank,0L,0);
> fread(buf,512,1,bank);
> fseek(bank,0L,0);
>
> You read the data from it then to back to the start.
>
> //fflush(bank); //uncoment this for bugfix
>
> ftruncate( fileno(bank), 0 );

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.

>
>
> 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?

>
>
> Any behaviour now is undefined because you are mixing stdio and non-stdio
> on the same descriptor without proper caution.
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majo...@vger.rutgers.edu
> Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html

--

Alex Riedel

--------------C0E1DEFC3CAA3EE0A83DC3F8


Content-Type: text/html; charset=koi8-r
Content-Transfer-Encoding: 7bit

<HTML>
Marc Slemko wrote:
<BLOCKQUOTE TYPE=CITE>Why do you think there is any bug?

<P>[...]
<BR>&nbsp;bank = fopen("test","r+");

<P>You open a file using stdio.

<P>&nbsp;fseek(bank,0L,0);
<BR>&nbsp;fread(buf,512,1,bank);
<BR>&nbsp;fseek(bank,0L,0);

<P>You read the data from it then to back to the start.

<P>&nbsp;//fflush(bank); //uncoment this for bugfix

<P>&nbsp;ftruncate( fileno(bank), 0 );</BLOCKQUOTE>
fseek(bank,0L,0); //back to the startfwrite(buf,512,1,bank); //i write
data to start
<BR>//hier file test is 512 bytes len.
<BR>lseek(fileno(bank),0L,2); //go to the end
<BR>//after lseek file test truncated.

<P>On Solaris without "fflush" this works fine.
<BR>This is not my program. I just ported this prog. from Solaris to Linux.
<BLOCKQUOTE TYPE=CITE>&nbsp;

<P>You do something bad by improperly using stdio; before you can do anything
<BR>with fileno() you need to fflush the stream.&nbsp; The Solaris man
page for
<BR>stdio has a reasonable discussion of this.</BLOCKQUOTE>
This is feature? or bug?
<BLOCKQUOTE TYPE=CITE>&nbsp;

<P>Any behaviour now is undefined because you are mixing stdio and non-stdio
<BR>on the same descriptor without proper caution.

<P>-
<BR>To unsubscribe from this list: send the line "unsubscribe linux-kernel"
in
<BR>the body of a message to majo...@vger.rutgers.edu
<BR>Please read the FAQ at <A HREF="http://www.altern.org/andrebalsa/doc/lkml-faq.html">http://www.altern.org/andrebalsa/doc/lkml-faq.html</A></BLOCKQUOTE>
&nbsp;
<PRE>--&nbsp;

Alex Riedel</PRE>
&nbsp;</HTML>

--------------C0E1DEFC3CAA3EE0A83DC3F8--

Brandon S. Allbery KF8NH

unread,
Aug 24, 1998, 3:00:00 AM8/24/98
to
(I had originally replied privately, but I'll tag onto the on-list
discussion instead.)

In message <35E0EE92...@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] all...@kf8nh.apk.net
system administrator [WAY too many hats] all...@ece.cmu.edu
electrical and computer engineering KF8NH
carnegie mellon university

Brandon S. Allbery KF8NH

unread,
Aug 24, 1998, 3:00:00 AM8/24/98
to
In message <35E0DB62...@taraz.kz>, Alex Riedel writes:
+-----
| Please test this program.
| After executing this program file 'test' is 0 bytes lenght.
+--->8

libc only guarantees the effect of mixed raw and buffered I/O after an
fflush(). It's the program that is broken, not libc and not the kernel;
without the fflush() the behavior of the ftruncate() and lseek() is
*undefined*.

david

unread,
Aug 25, 1998, 3:00:00 AM8/25/98
to
Reply to mail from Alex Riedel about Bug in kernel or libc !?
-----------------
>
> --------------153D7D566EE92E2982905088

> Content-Type: text/plain; charset=koi8-r
> Content-Transfer-Encoding: 7bit
>
> Hello!

>
> Please test this program.
>
> After executing this program file 'test' is 0 bytes lenght.
>
> On Solaris 2.5 x86 this works fine.
>
> --
> Alex Riedel
>
>
>
> --------------153D7D566EE92E2982905088
> Content-Type: text/html; charset=koi8-r
> Content-Transfer-Encoding: 7bit
>
> <HTML>
> Hello!
> <PRE>Please test this program.</PRE>
>
> <PRE>After executing this program file 'test' is 0 bytes lenght.</PRE>
>
> <PRE>On Solaris 2.5 x86 this works fine.</PRE>
>
> <PRE>--&nbsp;
> Alex Riedel</PRE>
> &nbsp;</HTML>
>
> --------------153D7D566EE92E2982905088--
>


Please turn off the HTML formatting. It is redundant; sending two copies
of your mail to everyone. Many of us are not using a m$ inflicted product
nor do we use netscape for email. Thus we do not see your post because it
is an "attachment". It's quite perturbing to go dig it out of a mailfile
and cut/paste. Many of us simply filter these kinds of mail to /dev/null.

To the linux-kernel FAQ maintainer, if this is not on the FAQ, please add
a blurb.

thankyou,
-d
--
Look, look, see Windows 98. Buy, lemmings, buy!
(c) 1998 David Ford. Redistribution via the Microsoft Network is prohibited.
for linux-kernel: please read linux/Documentation/* before posting problems

0 new messages