This is the mail archive of the mailing list for the glibc 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]

[Bug libc/3746] New: exit violates POSIX on seekable stdin

POSIX requires in XCU 1.11:

"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....

    tail -n +2 file
    (sed -n 1q; cat) < file
    cat file | (sed -n 1q; cat)

The second command is equivalent to the first only when the file is seekable."

POSIX also requires that "All of the file descriptors, directory streams, [XSI]
[Option Start]  conversion descriptors, and message catalog descriptors [Option
End]  open in the calling process shall be closed" on exit.

Both of these conditions are violated with glibc:

$ echo 1 2 3 | tr ' ' '\n' > file
$ (sed -n 1q; cat) < file
$ strace sed -ne 1q < file 2>&1 |tail
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) = 0xb7dcc000
close(3)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?
Process 21067 detached

Oops - the process failed to lseek back to offset 2, which is the next byte not
processed by sed; and failed to call close(0).

A utility that is worried about POSIX compliance can use the workaround of
manually calling fflush(stdin), fseek(stdin, 0, SEEK_CUR), fclose(stdin) prior
to exit(), but this should be taken care of by libc, as is the case on other
platforms such as Solaris.

           Summary: exit violates POSIX on seekable stdin
           Product: glibc
           Version: 2.3.4
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: ebb9 at byu dot net
                CC: glibc-bugs at sources dot redhat dot com

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.

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