bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: failure with HEAD: stdin seekable


From: Jim Meyering
Subject: Re: failure with HEAD: stdin seekable
Date: Tue, 19 Dec 2006 09:54:31 +0100

Eric Blake <address@hidden> wrote:
> Solaris 8 gets this particular test correct, as does CVS cygwin.  So your
> failure report means that we have just uncovered a glibc bug.  POSIX
> requires the following behavior when stdin is seekable:
>
> $ echo 1 2 3 | tr ' ' '\n' > file
> $ (sed -ne 1q; cat) < file
> 2
> 3
>
> But glibc mistakenly optimizes exit() and fails to close stdin:

Isn't the problem more with the missing seek in sed?
Since sed calls fread to read a 4KB buffer,
it should reposition the file pointer for stdin.

> $ strace sed -ne 1q < file 2>&1 | tail
> mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
> = 0xb7db9000
> read(0, "1\n2\n3\n", 4096)              = 6
> open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
> fstat64(3, {st_mode=S_IFREG|0644, st_size=21544, ...}) = 0
> mmap2(NULL, 21544, PROT_READ, MAP_SHARED, 3, 0) = 0xb7db3000
> close(3)                                = 0
> close(1)                                = 0
> close(2)                                = 0
> exit_group(0)                           = ?
> Process 11905 detached
>
> Oops - fd 0 was never closed, let alone reset to the last unbuffered
> offset, so the cat process is starting from the wrong offset.  It is this
> same bug that is breaking the m4 test.  I will file a glibc bug report.
>
> In the meantime, gnulib can probably work around this bug.  m4 already
> uses the closeout module.  I think close_stdout be updated to flush and
> close stdin for the benefit of implementations (like glibc, or like cygwin
> 1.5.22 and earlier) where exit() is broken because it fails to reset the
> fd offset of seekable stdin.

IMHO, robust software cannot rely on exit to close stdin, since
if there has been a read error, exit certainly won't diagnose it.
However, you have to be careful to close stdin only if you've used it.
This latter is perhaps reason enough not to make closeout close stdin.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]