[Bug stdio/19476] New: fgetc reads from a stream at EOF

msebor at redhat dot com sourceware-bugzilla@sourceware.org
Fri Jan 15 23:29:00 GMT 2016


https://sourceware.org/bugzilla/show_bug.cgi?id=19476

            Bug ID: 19476
           Summary: fgetc reads from a stream at EOF
           Product: glibc
           Version: 2.23
            Status: NEW
          Severity: normal
          Priority: P2
         Component: stdio
          Assignee: unassigned at sourceware dot org
          Reporter: msebor at redhat dot com
  Target Milestone: ---

C and POSIX require that fgetc() fail with EOF when the stream's EOF state is
set.  The test case below shows that glibc violates this requirement.

Now that I've spent time creating a test case and was about to submit the bug I
noticed (thanks to Bugzilla's Possible Duplicates feature) that there are
already are a number of bugs about this: the oldest one being bug 1190, still
in an Assigned state, plus bug 12351 Resolved as Worksforsome.

Since I think ths test case is easier to integrate into the test suite than any
of the others I will go ahead and submit this bug anyway and close it as a
duplicate.  If it's ever fixed, the test case can be used.

#include <assert.h>
#include <errno.h>
#include <stdio.h>

int main (void)
{
  errno = 0;
  FILE *fout = fopen ("/tmp/testfile", "w");
  if (!fout) {
    printf ("fopen failed for writing: %s\n", strerror (errno));
    return 1;
  }

  fprintf (fout, "abc\n");
  fflush (fout);

  FILE *fin = fopen ("/tmp/testfile", "r");
  if (!fin) {
    printf ("fopen failed for reading: %s\n", strerror (errno));
    return 1;
  }

  // Reach EOF.
  while (EOF != fgetc (fin))
    ;

  if (ferror (fin)) {
    printf ("ferror: %s\n", strerror (errno));
    return 1;
  }

  // Append to the file.
  if (2 != fprintf (fout, "d\n")) {
    printf ("fprintf failed: %s\n", strerror (errno));
    return 1;
  }

  fclose (fout);

  // Verify the input stream is at EOF.
  assert (feof (fin));

  /* From SUSv4, fgetc:

   DESCRIPTION
       If the end-of-file indicator for the input stream pointed to by
       stream is not set and a next byte is present, the fgetc() function
       shall obtain the next byte...

   RETURN VALUE
       If the end-of-file indicator for the stream is set, or if the stream
       is at end-of-file, the end-of-file indicator for the stream shall be
       set and fgetc() shall return EOF.  */

  int c = fgetc (fin);

  printf ("fgetc() = %i, feof() = %i\n", c, feof (fin));
  assert (EOF == c && feof (fin));

  return 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.


More information about the Glibc-bugs mailing list