🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #6200

Bug in libc 5.0.9 !!

#6200
From: Thomas Leitner
Date: Tue, 09 Jan 1996 00:00
111 lines
4055 bytes
[ Article reposted from comp.os.linux.development.apps ]
[ Author was Thomas Leitner ]
[ Posted on 9 Jan 1996 10:18:23 GMT ]

Hi, 

We found the following bug in libc 5.0.9 when we ported our app. to Linux:
When a file is opened by two processes where one process repeatedly writes 
a data block with fwrite and the other process reads it with fread, the 
reader process does *not* get the actual data from the file. It gets the
old data from the stream buffer instead which is *wrong*. I tried this on
all sorts of Unixes I could get an account on at our campus (OSF/1 3.2,
Solaris 2.4, Ultrix 4.4, SunOS 4.1.3_U1) and it works as its supposed
to but on Linux (Slackware 3.0) with libc 5.0.9, the same data is
read all over again. 

Note that the fix for this is to set the input stream to unbuffered
using "setbuf(fi, NULL);".

Comments anyone?

Tom

P.S: Here's my test program. Start writer process with "lbug" and
     reader process with "lbug read".

/* lbug.c: demonstrates a bug in libc 5.0.9 under Linux */

#define _POSIX_SOURCE   1
#include <stdlib.h>
#include <stdio.h>

void main(int argc, char *argv[])
{
    /* if more than one argument given -> READ the file */
    
    if (argc > 1) {
    	int i;
    	char buffer[512];
        FILE *fi = fopen("test.dat", "rb");
        if (fi == (FILE*) NULL) {
            perror("fopen(test.dat)");
            exit(1);
        }

        /* THIS IS NEEDED UNDER LINUX W/ LIBC 5.0.9  TO GET IT GOING */

        /*setbuf(fi, NULL);*/

        while (1) {
            if (fseek(fi, 0L, SEEK_SET) != 0) {
            	perror("fseek");
            	exit(1);
            }
            if (fread(buffer, 512, 1, fi) != 1) {
            	perror("fread");
            	exit(1);
            }
            for (i = 0; i < 512; i++) {
                if (buffer[0] != buffer[i]) printf("BUFFER ERROR\n");
            }
            printf("read buffer %d\n", (int) buffer[0]);
            sleep(1);
        }
    } else {

        /* no argument given -> WRITE the file */

    	int i = 0;
    	char buffer[512];
        FILE *fo = fopen("test.dat", "wb");
        if (fo == (FILE*) NULL) {
            perror("fopen(test.dat)");
            exit(1);
        }
        while (1) {
            i++;
            i = i % 256;
            memset(buffer, i, 512);
            if (fseek(fo, 0L, SEEK_SET) != 0) {
            	perror("fseek");
            	exit(1);
            }
            if (fwrite(buffer, 512, 1, fo) != 1) {
            	perror("fwrite");
            	exit(1);
            }
            printf("wrote buffer %d\n", i);
            sleep(1);
        }
    }
}

/* E O F */

-- 
Tom Leitner, tom@finwds01.tu-graz.ac.at              |\      _,,,---,,_
Dept. of Communications                         zzz. /,`.-'`'    -.  ;-;;,_.
Graz University of Technology                       |,4-  ) )-,_. ,\ (  `'-'
Inffeldgasse 12                                    '---''(_/--'  `-'\_)
A-8010 Graz / Austria / Europe, Phone: +43-316-873-7455, Fax: +43-316-463-697
WWW home page: http://wiis.tu-graz.ac.at/people/tom.html

-- 
Tom Leitner, tom@finwds01.tu-graz.ac.at              |\      _,,,---,,_
Dept. of Communications                         zzz. /,`.-'`'    -.  ;-;;,_.
Graz University of Technology                       |,4-  ) )-,_. ,\ (  `'-'
Inffeldgasse 12                                    '---''(_/--'  `-'\_)
A-8010 Graz / Austria / Europe, Phone: +43-316-873-7455, Fax: +43-316-463-697
WWW home page: http://wiis.tu-graz.ac.at/people/tom.html
Technical Details
Message-ID: <4ctffu$i4s@fstgal00.tu-graz.ac.at>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 113d02,3f0dc47e34a46674,start
X-Google-Attributes: gid113d02,public
From: tom@finwds01.tu-graz.ac.at (Thomas Leitner)
Subject: Bug in libc 5.0.9 !!
Date: 1996/01/09
Message-ID: <4ctffu$i4s@fstgal00.tu-graz.ac.at>
X-Deja-AN: 135045611
distribution: gnu
sender: daemon@cis.ohio-state.edu
organization: Graz University of Technology, Austria
newsgroups: gnu.gcc.bug