public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/12724] New: fclose violates POSIX 2008 on seekable input streams
@ 2011-05-02 20:21 eblake at redhat dot com
  2011-05-14  1:11 ` [Bug libc/12724] " drepper.fsp at gmail dot com
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: eblake at redhat dot com @ 2011-05-02 20:21 UTC (permalink / raw)
  To: glibc-bugs

http://sourceware.org/bugzilla/show_bug.cgi?id=12724

           Summary: fclose violates POSIX 2008 on seekable input streams
           Product: glibc
           Version: 2.13
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper.fsp@gmail.com
        ReportedBy: eblake@redhat.com


POSIX 2008 states:

"If the file is not already at EOF, and the file is one capable of seeking, the
file offset of the underlying open file description shall be adjusted so that
the next operation on the open file description deals with the byte after the
last one read from or written to the stream being closed."

[XSH fclose,
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html]

However, this sample program proves that for seekable input streams, glibc is
violating this requirement.  The same program runs to completion on Solaris 10.


#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#define NAME "test-fclose.t"

int
main (void)
{
  const char buf[] = "hello world";
  int fd;
  int fd2;
  FILE *f;

  /* Prepare a seekable file.  */
  fd = open (NAME, O_RDWR | O_CREAT | O_TRUNC, 0600);
  assert (0 <= fd);
  assert (write (fd, buf, sizeof buf) == sizeof buf);
  assert (lseek (fd, 1, SEEK_SET) == 1);

  /* Create an output stream visiting the file; when it is closed, all
     other file descriptors visiting the file must see the new file
     position.  */
  fd2 = dup (fd);
  assert (0 <= fd2);
  f = fdopen (fd2, "w");
  assert (f);
  assert (fputc (buf[1], f) == buf[1]);
  assert (fclose (f) == 0);
  errno = 0;
  assert (lseek (fd2, 0, SEEK_CUR) == -1);
  assert (errno == EBADF);
  assert (lseek (fd, 0, SEEK_CUR) == 2);

  /* Likewise for an input stream.  */
  fd2 = dup (fd);
  assert (0 <= fd2);
  f = fdopen (fd2, "r");
  assert (f);
  assert (fgetc (f) == buf[2]);
  assert (fclose (f) == 0);
  errno = 0;
  assert (lseek (fd2, 0, SEEK_CUR) == -1);
  assert (errno == EBADF);
  assert (lseek (fd, 0, SEEK_CUR) == 3);

  /* Clean up.  */
  assert (remove (NAME) == 0);

  return 0;
}

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2020-04-14  0:19 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-02 20:21 [Bug libc/12724] New: fclose violates POSIX 2008 on seekable input streams eblake at redhat dot com
2011-05-14  1:11 ` [Bug libc/12724] " drepper.fsp at gmail dot com
2011-07-27 17:34 ` eblake at redhat dot com
2011-08-12 15:31 ` matz at suse dot de
2011-09-23  1:30 ` vapier at gentoo dot org
2012-02-21  2:39 ` [Bug stdio/12724] " jsm28 at gcc dot gnu.org
2012-07-03  5:24 ` vapier at gentoo dot org
2012-11-29 15:19 ` carlos_odonell at mentor dot com
2012-12-01 19:56 ` carlos_odonell at mentor dot com
2012-12-03 23:57 ` carlos at systemhalted dot org
2012-12-11 11:48 ` hannes at stressinduktion dot org
2014-01-17 22:06 ` eblake at redhat dot com
2014-01-17 22:10 ` eblake at redhat dot com
2014-01-17 23:11 ` joseph at codesourcery dot com
2014-06-27 13:27 ` fweimer at redhat dot com
2020-04-14  0:18 ` eblake at redhat dot com
2020-04-14  0:19 ` eblake at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).