This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fwd: stdio bug


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Originally reported to cygwin, but I think this is a newlib bug.  I
noticed that fflush sets __SNPT when the file needs an lseek before the
next read or write; I think that making fclose perform the lseek after
fflush if __SNPT is set will resolve the situation.  But I haven't had
time to test a patch along these lines yet.

- -------- Original Message --------
Subject: stdio bug
Date: Thu, 07 Sep 2006 21:24:33 +0000 (UTC)
From: Eric Blake
To: cygwin

POSIX requires "When a standard utility reads a seekable input file and
terminates without an error before it reaches end-of-file, the utility
shall ensure that the file offset in the open file description is properly
positioned just past the last byte processed by the utility."

For an example, POSIX requires these two commands to give the same results:
$ echo 1 2 3 | tr ' ' '\n' > file
$ tail -n +2 file
2
3
$ (sed -n 1q; cat) < file
2
3

On cygwin, this isn't happening:

$ (sed -n 1q; cat) < file
$

But on Solaris, it is:

$ (truss sed -n 1q; cat) < file 2>&1 | tail
fstat64(0, 0xFFBEED40)                          = 0
brk(0x00042100)                                 = 0
brk(0x00044100)                                 = 0
read(0, " 1\n 2\n 3\n", 8192)                   = 6
close(1)                                        = 0
close(2)                                        = 0
llseek(0, 0xFFFFFFFFFFFFFFFC, SEEK_CUR)         = 2
_exit(0)
2
3

Notice the llseek(0,-4,SEEK_CUR) just before _exit, which restored the
file pointer back to the last character consumed, so that cat could then
start up from the right location.

I don't know whether a fix to this would lie in fclose() (making sure that
on input streams, the position of the underlying fd is restored when
discarding buffered data), or somewhere else, but it would be nice to see
this fixed.  And it seems like the bug is in cygwin, not sed, since it is
the same version of GNU sed on both machines.

- --
Eric Blake
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFBWXF84KuGfSFAYARAqzuAJ4lKjrDRlSy+Fij27uvCFSYls2khwCgjM5C
MQh5DXfxsVnyNBdfry2SX4Y=
=8IV6
-----END PGP SIGNATURE-----


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]