new module 'freadseek'

Jeff Johnston jjohnstn@redhat.com
Mon Mar 3 21:51:00 GMT 2008


Eric Blake wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> According to Eric Blake on 3/1/2008 1:36 PM:
> | On further investigation, I think this is a true bug in newlib's stdio,
> | and not in your code.
>
> Newlib has two bugs - first, fflush is failing to discard ungetc data 
> when
> changing the underlying fd offset.  Second, ftell is improperly calling
> fflush under the hood, which when coupled with the first bug, results 
> in a
> confused file offset, but even when the first bug is fixed, loses data
> contrary to POSIX.  (BSD's ftell probably also calls fflush under the
> hood, but since BSD's fflush disobeys POSIX by treating fflush on a read
> stream as a no-op rather than discarding unread data, these bugs are not
> visible there).
>
> $ cat foo.c
> #include <stdio.h>
> #include <stdlib.h>
>
> int
> main (int argc, char* argv[])
> {
> ~  int i1, i2;
> ~  i1 = fgetc (stdin);
> ~  printf ("i1=%c\n", i1);
> ~  i1 = ungetc ('@', stdin);
> ~  printf ("i1=%c\n", i1);
> ~  if (argc > 1)
> ~    {
> ~      i2 = ftell (stdin);
> ~      printf ("i2=%d\n", i2);
> ~    }
> ~  i1 = fgetc (stdin);
> ~  printf ("i1=%c\n", i1);
> ~  return 0;
> }
> $ ./foo < foo.c
> i1=#
> i1=@
> i1=@
> $ ./foo a < foo.c
> i1=#
> i1=@
> i2=-337
> i1=i
>
> On Linux, and after my patch, it properly does:
> % ./foo < foo.c
> i1=#
> i1=@
> i1=@
> % ./foo a < foo.c
> i1=#
> i1=@
> i2=0
> i1=@
>
> OK to apply this to newlib?  Patching gnulib to work around these newlib
> bugs for older releases of cygwin seems daunting; it's hard to do 
> anything
> when you can't trust ftell or fflush after ungetc.
>
Yes, thanks.

-- Jeff J.
> 2008-03-01  Eric Blake  <ebb9@byu.net>
>
>     Fix ftell bug after ungetc.
>     * libc/stdio/ftell.c (_ftell_r): Don't flush ungetc buffer on
>     ftell.
>     * libc/stdio64/ftello64.c (_ftello64_r): Likewise.
>     * libc/stdio/fflush.c (_fflush_r): Clear unget buffer when
>     repositioning underlying fd offset.
>
> - --
> Don't work too hard, make some time for fun as well!
>
> Eric Blake             ebb9@byu.net
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.5 (Cygwin)
> Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFHyec984KuGfSFAYARAoezAKDC6snVwkdf1ZcSS0NbgTZ5fRMLBACaAnSl
> Cixn14d6TZ56/1PH6WgR10A=
> =/Dr1
> -----END PGP SIGNATURE-----



More information about the Newlib mailing list