This is the mail archive of the glibc-bugs@sourceware.org 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 stdio/16532] New: fdopen(..., "a"); fwrite(); ftell() sequence gives wrong answer


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

            Bug ID: 16532
           Summary: fdopen(..., "a"); fwrite(); ftell() sequence gives
                    wrong answer
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: normal
          Priority: P2
         Component: stdio
          Assignee: unassigned at sourceware dot org
          Reporter: ppluzhnikov at google dot com

This is a regression from 2.15.

Test case below, does not assert with glibc-2.15, but asserts with current
trunk.

If I use fopen() instead of fdopen() ["./a.out -o"] or if I fflush() after
fwrite() ["./a.out -f"], then assert goes away.

/* --- cut --- */
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  size_t written;
  off_t off;
  int do_flush = 0, do_fopen = 0;
  int opt;
  const char *const fname = "/tmp/output";

  while ((opt = getopt (argc, argv, "fo")) != -1)
    {
      switch (opt)
        {
        case 'f': do_flush = 1; break;
        case 'o': do_fopen = 1; break;
        }
    }

  fp = fopen (fname, "w");
  written = fwrite ("abcabc", 1, 6, fp);
  assert (written == 6);

  fclose (fp);

  if (do_fopen)
    fp = fopen (fname, "a");
  else
    {
      int fd = open (fname, O_WRONLY, 0);
      assert (fd != -1);
      fp = fdopen (fd, "a");
    }

  assert (fp != NULL);

  written = fwrite ("ghi", 1, 3, fp);
  assert (written == 3);

  if (do_flush)
    fflush (NULL);

  off = ftello (fp);
  assert (off == 9);

  return 0;
}
/* --- cut --- */

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


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