public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug stdio/17063] New: fclose() may fail to flush data
@ 2014-06-17 17:35 ppluzhnikov at google dot com
  2014-06-17 17:36 ` [Bug stdio/17063] " ppluzhnikov at google dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: ppluzhnikov at google dot com @ 2014-06-17 17:35 UTC (permalink / raw)
  To: glibc-bugs

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

            Bug ID: 17063
           Summary: fclose() may fail to flush data
           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 followup to PR16532.

A program that does

  fopen(..., "w+");
  fwrite(...);
  fread(...);
  fclose();

may leave empty file on disk (fail to flush when it should have), when the
fread requests more than a page's worth of data.

At least glibc-2.19 through current trunk
(754c5a08aacb44895d1ab97c553ce424eb43f761) are affected.

./t
testing     3
testing     3 OK
testing     6
testing     6 OK
testing     9
testing     9 OK
testing  4095
testing  4095 OK
testing  4096
t: t.c:28: do_test: Assertion `pos == 6' failed.
Aborted

/// --- cut ---
#include <assert.h>
#include <stdio.h>

void do_test (int n)
{
  FILE *fp;
  const char *const fname = "/tmp/output";
  int nwritten, nread, pos;
  char line[8192];

  printf ("testing %5d\n", n);
  fp = fopen (fname, "w+");
  nwritten = fwrite ("abcabc", 1, 6, fp);
  assert (nwritten == 6);

  pos = ftello (fp);
  assert (pos == 6);

  nread = fread (line, 1, n, fp);
  assert (nread == 0);

  fclose (fp);

  fp = fopen (fname, "r");
  fseeko (fp, 0, SEEK_END);
  pos = ftello (fp);

  assert (pos == 6);
  printf ("testing %5d OK\n", n);
}

int main(int argc, char *argv[])
{
  int j, nreads[] = { 3, 6, 9, 4095, 4096, 4097, 8191, 8192 };

  for (j = 0; j < sizeof (nreads) / sizeof (nreads[0]); ++j)
    do_test (nreads[j]);

  return 0;
}

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


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

end of thread, other threads:[~2015-07-11  8:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-17 17:35 [Bug stdio/17063] New: fclose() may fail to flush data ppluzhnikov at google dot com
2014-06-17 17:36 ` [Bug stdio/17063] " ppluzhnikov at google dot com
2014-06-17 18:50 ` ppluzhnikov at google dot com
2014-06-17 18:50 ` ppluzhnikov at google dot com
2014-06-19 14:47 ` fweimer at redhat dot com
2014-06-23 14:55 ` ppluzhnikov at google dot com
2015-07-11  8:25 ` schwab@linux-m68k.org

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