Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sun, 17 Jun 2012 23:07:33 +0200
From: Szabolcs Nagy <nsz@...t70.net>
To: musl@...ts.openwall.com
Subject: Re: setvbuf, __fbufsize bug

* Bruno Haible <bruno@...sp.org> [2012-06-17 21:26:44 +0200]:
> With musl-0.9.1: This program shows that after calling setvbuf to make
> a stream buffered, it may in fact be unbuffered (__fbufsize returns 0).
> 

this is a broken test in gnulib
see c99 7.19.5.6:

"The setvbuf function may be used only after the stream pointed to by
stream has been associated with an open file and before any other operation
(other than an unsuccessful call to setvbuf) is performed on the stream."


> #include <stdio.h>
> #include <stdio_ext.h>
> #include <stdlib.h>
> 
> #define TESTFILE "t-fbufmode.tmp"
> 
> int
> main ()
> {
>   FILE *fp;
>   char buf[5];
> 
>   /* Create a file with some contents.  */
>   fp = fopen (TESTFILE, "w");
>   if (fp == NULL)
>     goto skip;
>   if (fwrite ("foobarsh", 1, 8, fp) < 8)
>     goto skip;
>   if (fclose (fp))
>     goto skip;
> 
>   /* Open it for reading.  */
>   fp = fopen (TESTFILE, "r");
> 
>   if (setvbuf (fp, NULL, _IONBF, 0))
>     goto skip;
> 
>   if (__fbufsize (fp) > 0)
>     {
>       fprintf (stderr, "in unbuffered mode, __fbufsize returns > 0 !\n");
>       exit (1);
>     }
> 
>   if (setvbuf (fp, buf, _IOFBF, 5) == 0)

this call is UB

>     {
>       if (__fbufsize (fp) == 0)
>         {
>           fprintf (stderr, "in buffered mode, __fbufsize returns 0 !\n");
>           exit (1);
>         }
>     }
> 
>   fclose (fp);
> 
>   return 0;
> 
>  skip:
>   fprintf (stderr, "Skipping test: file operations failed.\n");
>   return 77;
> }
> ======================================================================
> $ musl-gcc foo.c -Wall
> $ ./a.out
> in buffered mode, __fbufsize returns 0 !

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.