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

* [Bug stdio/17063] fclose() may fail to flush data
  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 ` ppluzhnikov at google dot com
  2014-06-17 18:50 ` ppluzhnikov at google dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ppluzhnikov at google dot com @ 2014-06-17 17:36 UTC (permalink / raw)
  To: glibc-bugs

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

Paul Pluzhnikov <ppluzhnikov at google dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |siddhesh at redhat dot com

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


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

* [Bug stdio/17063] fclose() may fail to flush data
  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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ppluzhnikov at google dot com @ 2014-06-17 18:50 UTC (permalink / raw)
  To: glibc-bugs

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

--- Comment #1 from Paul Pluzhnikov <ppluzhnikov at google dot com> ---
Google ref: b/15017950

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


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

* [Bug stdio/17063] fclose() may fail to flush data
  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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ppluzhnikov at google dot com @ 2014-06-17 18:50 UTC (permalink / raw)
  To: glibc-bugs

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

Paul Pluzhnikov <ppluzhnikov at google dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bmoses at google dot com

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


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

* [Bug stdio/17063] fclose() may fail to flush data
  2014-06-17 17:35 [Bug stdio/17063] New: fclose() may fail to flush data ppluzhnikov at google dot com
                   ` (2 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: fweimer at redhat dot com @ 2014-06-19 14:47 UTC (permalink / raw)
  To: glibc-bugs

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

Florian Weimer <fweimer at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
              Flags|                            |security-

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


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

* [Bug stdio/17063] fclose() may fail to flush data
  2014-06-17 17:35 [Bug stdio/17063] New: fclose() may fail to flush data ppluzhnikov at google dot com
                   ` (3 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: ppluzhnikov at google dot com @ 2014-06-23 14:55 UTC (permalink / raw)
  To: glibc-bugs

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

Paul Pluzhnikov <ppluzhnikov at google dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Paul Pluzhnikov <ppluzhnikov at google dot com> ---
From: Siddhesh Poyarekar <siddhesh@redhat.com>
---
That's not a bug because you're invoking a behaviour explicitly
forbidden by the standard:

    When a file is opened with update mode ( '+' as the second or
    third character in the mode argument), both input and output may
    be performed on the associated stream. However, the application
    shall ensure that output is not directly followed by input without
    an intervening call to fflush() or to a file positioning function
    ( fseek(), fsetpos(), or rewind()), and input is not directly
    followed by output without an intervening call to a file
    positioning function, unless the input operation encounters
    end-of-file.

We forbid it in the manual too:

    As you can see, ‘+’ requests a stream that can do both input and
    output. When using such a stream, you must call fflush (see Stream
    Buffering) or a file positioning function such as fseek (see File
    Positioning) when switching from reading to writing or vice
    versa. Otherwise, internal buffers might not be emptied properly.

If you add an fseek call before ftello, you'll see the correct
behaviour because fseek acts as the necessary barrier between fread
and fwrite.  ftell is not good enough for that because it cannot be
relied on to change fp state.
---

Thanks for analysis.

This is quite end-user unfriendly behavior:
- it's new ("broke" somewhere between glibc-2.15 and 2.19),
- it's "broken" only for some, but not all, calls to output followed by input
without intervening flush.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
>From glibc-bugs-return-24012-listarch-glibc-bugs=sources.redhat.com@sourceware.org Mon Jun 23 15:15:23 2014
Return-Path: <glibc-bugs-return-24012-listarch-glibc-bugs=sources.redhat.com@sourceware.org>
Delivered-To: listarch-glibc-bugs@sources.redhat.com
Received: (qmail 8094 invoked by alias); 23 Jun 2014 15:15:22 -0000
Mailing-List: contact glibc-bugs-help@sourceware.org; run by ezmlm
Precedence: bulk
List-Id: <glibc-bugs.sourceware.org>
List-Subscribe: <mailto:glibc-bugs-subscribe@sourceware.org>
List-Post: <mailto:glibc-bugs@sourceware.org>
List-Help: <mailto:glibc-bugs-help@sourceware.org>, <http://sourceware.org/lists.html#faqs>
Sender: glibc-bugs-owner@sourceware.org
Delivered-To: mailing list glibc-bugs@sourceware.org
Received: (qmail 8059 invoked by uid 48); 23 Jun 2014 15:15:17 -0000
From: "siddhesh at redhat dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug stdio/17063] fclose() may fail to flush data
Date: Mon, 23 Jun 2014 15:15:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: glibc
X-Bugzilla-Component: stdio
X-Bugzilla-Version: unspecified
X-Bugzilla-Keywords:
X-Bugzilla-Severity: normal
X-Bugzilla-Who: siddhesh at redhat dot com
X-Bugzilla-Status: RESOLVED
X-Bugzilla-Priority: P2
X-Bugzilla-Assigned-To: unassigned at sourceware dot org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags: security-
X-Bugzilla-Changed-Fields:
Message-ID: <bug-17063-131-aorCzupQmf@http.sourceware.org/bugzilla/>
In-Reply-To: <bug-17063-131@http.sourceware.org/bugzilla/>
References: <bug-17063-131@http.sourceware.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://sourceware.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2014-06/txt/msg01388.txt.bz2
Content-length: 799

https://sourceware.org/bugzilla/show_bug.cgi?id\x17063

--- Comment #3 from Siddhesh Poyarekar <siddhesh at redhat dot com> ---
(In reply to Paul Pluzhnikov from comment #2)
> This is quite end-user unfriendly behavior:
> - it's new ("broke" somewhere between glibc-2.15 and 2.19),
> - it's "broken" only for some, but not all, calls to output followed by
> input without intervening flush.

Yes, it broke when I wrote a new implementation of ftell.  The earlier
implementation was essentially an alias for fseek (fp, 0, SEEK_CUR), which is
why it worked as an intermediate function to switch between reads and writes.
I didn't bother versioning the change because we explicitly forbid the
behaviour this bug relies on.

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


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

* [Bug stdio/17063] fclose() may fail to flush data
  2014-06-17 17:35 [Bug stdio/17063] New: fclose() may fail to flush data ppluzhnikov at google dot com
                   ` (4 preceding siblings ...)
  2014-06-23 14:55 ` ppluzhnikov at google dot com
@ 2015-07-11  8:25 ` schwab@linux-m68k.org
  5 siblings, 0 replies; 7+ messages in thread
From: schwab@linux-m68k.org @ 2015-07-11  8:25 UTC (permalink / raw)
  To: glibc-bugs

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

--- Comment #4 from Andreas Schwab <schwab@linux-m68k.org> ---
*** Bug 18659 has been marked as a duplicate of this bug. ***

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