public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/12799] New: fflush violates POSIX on seekable input streams
@ 2011-05-23 17:14 eblake at redhat dot com
  2012-02-21  2:23 ` [Bug stdio/12799] " jsm28 at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: eblake at redhat dot com @ 2011-05-23 17:14 UTC (permalink / raw)
  To: glibc-bugs

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

           Summary: fflush violates POSIX 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


Per http://austingroupbugs.net/view.php?id=87, fflush is required to discard
any ungetc() bytes and set the underlying file position to the point as though
the pushed-back bytes had never been read.  That is, in the sequence:

{ app1; app2; } < seekable

if app1 reads one byte too many, then does ungetc() to push it back, then app2
should start reading at the byte that app1 did not want, rather than at the
point that app1 reached before using ungetc().  More concretely, this program
should exit with status 0, but right now it is exiting with status 7 (glibc is
reading 'w' instead of ' ' after the fflush):


#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
int main (void)
{
  FILE *f;
  char buffer[10];
  int fd;
  int c;

  f = fopen ("file", "w+");
  if (f == NULL)
    return 1;
  if (fputs ("hello world", f) == EOF)
    return 2;
  rewind (f);

  fd = fileno (f);
  if (fd < 0 || fread (buffer, 1, 5, f) != 5)
    return 3;
  c = fgetc (f);
  if (c != ' ')
    return 4;
  if (ungetc ('@', f) != '@')
    return 5;
  if (fflush (f) == EOF)
    return 6;
  if (fgetc (f) != c)
    return 7;
  return 0;
}

This program returns 0 on Solaris and Cygwin.

-- 
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] 13+ messages in thread

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
@ 2012-02-21  2:23 ` jsm28 at gcc dot gnu.org
  2012-10-29 14:00 ` siddhesh at redhat dot com
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jsm28 at gcc dot gnu.org @ 2012-02-21  2:23 UTC (permalink / raw)
  To: glibc-bugs

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

Joseph Myers <jsm28 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|libc                        |stdio

-- 
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] 13+ messages in thread

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
  2012-02-21  2:23 ` [Bug stdio/12799] " jsm28 at gcc dot gnu.org
@ 2012-10-29 14:00 ` siddhesh at redhat dot com
  2012-12-11 11:48 ` hannes at stressinduktion dot org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: siddhesh at redhat dot com @ 2012-10-29 14:00 UTC (permalink / raw)
  To: glibc-bugs

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

Siddhesh Poyarekar <siddhesh at redhat dot com> changed:

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

-- 
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] 13+ messages in thread

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
  2012-02-21  2:23 ` [Bug stdio/12799] " jsm28 at gcc dot gnu.org
  2012-10-29 14:00 ` siddhesh at redhat dot com
@ 2012-12-11 11:48 ` hannes at stressinduktion dot org
  2012-12-19 10:48 ` schwab@linux-m68k.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: hannes at stressinduktion dot org @ 2012-12-11 11:48 UTC (permalink / raw)
  To: glibc-bugs

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

Hannes Frederic Sowa <hannes at stressinduktion dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hannes at stressinduktion
                   |                            |dot org

-- 
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] 13+ messages in thread

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (2 preceding siblings ...)
  2012-12-11 11:48 ` hannes at stressinduktion dot org
@ 2012-12-19 10:48 ` schwab@linux-m68k.org
  2014-06-27 13:17 ` fweimer at redhat dot com
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: schwab@linux-m68k.org @ 2012-12-19 10:48 UTC (permalink / raw)
  To: glibc-bugs

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

Andreas Schwab <schwab@linux-m68k.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|drepper.fsp at gmail dot    |unassigned at sourceware
                   |com                         |dot org

-- 
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] 13+ messages in thread

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (3 preceding siblings ...)
  2012-12-19 10:48 ` schwab@linux-m68k.org
@ 2014-06-27 13:17 ` fweimer at redhat dot com
  2020-04-14  0:18 ` eblake at redhat dot com
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: fweimer at redhat dot com @ 2014-06-27 13:17 UTC (permalink / raw)
  To: glibc-bugs

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

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] 13+ messages in thread

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (4 preceding siblings ...)
  2014-06-27 13:17 ` fweimer at redhat dot com
@ 2020-04-14  0:18 ` eblake at redhat dot com
  2020-04-14  0:24 ` eblake at redhat dot com
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: eblake at redhat dot com @ 2020-04-14  0:18 UTC (permalink / raw)
  To: glibc-bugs

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

Eric Blake <eblake at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |12724


Referenced Bugs:

https://sourceware.org/bugzilla/show_bug.cgi?id=12724
[Bug 12724] fclose violates POSIX 2008 on seekable input streams
-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (5 preceding siblings ...)
  2020-04-14  0:18 ` eblake at redhat dot com
@ 2020-04-14  0:24 ` eblake at redhat dot com
  2020-04-14 15:59 ` rjones at redhat dot com
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: eblake at redhat dot com @ 2020-04-14  0:24 UTC (permalink / raw)
  To: glibc-bugs

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

--- Comment #2 from Eric Blake <eblake at redhat dot com> ---
(In reply to Eric Blake from comment #1)
> The removal of libio.h is making this bug all the more important to fix, so
> that gnulib can quit mucking around in glibc internals:
> https://lists.gnu.org/archive/html/bug-gnulib/2018-03/msg00002.html
> 
> Is there a chance that it can be fixed for 2.28?

Still broken in 2020.  Similarly, fflush(NULL) should have this effect on all
seekable input streams.

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

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

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (6 preceding siblings ...)
  2020-04-14  0:24 ` eblake at redhat dot com
@ 2020-04-14 15:59 ` rjones at redhat dot com
  2020-08-02 23:05 ` chimpatheist at gmail dot com
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rjones at redhat dot com @ 2020-04-14 15:59 UTC (permalink / raw)
  To: glibc-bugs

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

Richard Jones <rjones at redhat dot com> changed:

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

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

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

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (7 preceding siblings ...)
  2020-04-14 15:59 ` rjones at redhat dot com
@ 2020-08-02 23:05 ` chimpatheist at gmail dot com
  2020-08-03 18:34 ` chimpatheist at gmail dot com
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: chimpatheist at gmail dot com @ 2020-08-02 23:05 UTC (permalink / raw)
  To: glibc-bugs

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

chimpatheist at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |chimpatheist at gmail dot com

--- Comment #3 from chimpatheist at gmail dot com ---
Maybe I'm wrong, but seems that this bug goes out if fflush() make a call to
_IO_switch_to_main_get_area() just before the call to _IO_new_file_sync().

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

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

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (8 preceding siblings ...)
  2020-08-02 23:05 ` chimpatheist at gmail dot com
@ 2020-08-03 18:34 ` chimpatheist at gmail dot com
  2020-08-03 20:03 ` chimpatheist at gmail dot com
  2021-01-22  2:47 ` dancol at dancol dot org
  11 siblings, 0 replies; 13+ messages in thread
From: chimpatheist at gmail dot com @ 2020-08-03 18:34 UTC (permalink / raw)
  To: glibc-bugs

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

--- Comment #4 from chimpatheist at gmail dot com ---
(In reply to chimpatheist from comment #3)
> Maybe I'm wrong, but seems that this bug goes out if fflush() make a call to
> _IO_switch_to_main_get_area() just before the call to _IO_new_file_sync().

Never mind.

Actually, the bug occurs when fflush() is called and _IO_IN_BACKUP is on, but
just returning to main_get_area doesn't work.

Before switching, we need calculate the difference between fp->_IO_read_end and
fp->_IO_read_ptr and adjust fp->_IO_read_ptr after the switch.

something like this :

fflush(FILE *fp)
{
     :
     :    
  if(fp->_flags & _IO_IN_BACKUP)
  {
     long delta = fp->_IO_read_end - fp->_IO_read_ptr;
     _IO_switch_to_main_get_area(fp);
     fp->_IO_read_base -= delta;
     fp->_IO_read_ptr = fp->_IO_read_base;
  }
     :
     :
}

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

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

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (9 preceding siblings ...)
  2020-08-03 18:34 ` chimpatheist at gmail dot com
@ 2020-08-03 20:03 ` chimpatheist at gmail dot com
  2021-01-22  2:47 ` dancol at dancol dot org
  11 siblings, 0 replies; 13+ messages in thread
From: chimpatheist at gmail dot com @ 2020-08-03 20:03 UTC (permalink / raw)
  To: glibc-bugs

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

--- Comment #5 from chimpatheist at gmail dot com ---
More exactly...

    if(fp->_flags & _IO_IN_BACKUP)
    {
        // how many ungetc() ?
        long delta1 = fp->_IO_read_end - fp->_IO_read_ptr;

        // how many fgetc() before going to backup area ?
        long delta2 = fp->_IO_save_base - fp->_IO_buf_base;

        // choose the better of the two values
        if(delta1 < delta2) delta2 = delta1;

        // discard all pushed chars
        fp->_IO_read_ptr = fp->_IO_read_end;

        // and reset save_base
        fp->_IO_save_base -= delta2;

        _IO_switch_to_main_get_area(fp);
    }

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

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

* [Bug stdio/12799] fflush violates POSIX on seekable input streams
  2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
                   ` (10 preceding siblings ...)
  2020-08-03 20:03 ` chimpatheist at gmail dot com
@ 2021-01-22  2:47 ` dancol at dancol dot org
  11 siblings, 0 replies; 13+ messages in thread
From: dancol at dancol dot org @ 2021-01-22  2:47 UTC (permalink / raw)
  To: glibc-bugs

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

Daniel Colascione <dancol at dancol dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dancol at dancol dot org

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

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

end of thread, other threads:[~2021-01-22  2:47 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-23 17:14 [Bug libc/12799] New: fflush violates POSIX on seekable input streams eblake at redhat dot com
2012-02-21  2:23 ` [Bug stdio/12799] " jsm28 at gcc dot gnu.org
2012-10-29 14:00 ` siddhesh at redhat dot com
2012-12-11 11:48 ` hannes at stressinduktion dot org
2012-12-19 10:48 ` schwab@linux-m68k.org
2014-06-27 13:17 ` fweimer at redhat dot com
2020-04-14  0:18 ` eblake at redhat dot com
2020-04-14  0:24 ` eblake at redhat dot com
2020-04-14 15:59 ` rjones at redhat dot com
2020-08-02 23:05 ` chimpatheist at gmail dot com
2020-08-03 18:34 ` chimpatheist at gmail dot com
2020-08-03 20:03 ` chimpatheist at gmail dot com
2021-01-22  2:47 ` dancol at dancol dot 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).