1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-08-07 06:43:00 +03:00

Fix fflush after ungetc on input file (bug 5994)

As discussed in bug 5994 (plus duplicates), POSIX requires fflush
after ungetc to discard pushed-back characters but preserve the file
position indicator.  For this purpose, each ungetc decrements the file
position indicator by 1; it is unspecified after ungetc at the start
of the file, and after ungetwc, so no special handling is needed for
either of those cases.

This is fixed with appropriate logic in _IO_new_file_sync.  I haven't
made any attempt to test or change things in this area for the "old"
functions; the case of files using mmap is addressed in a subsequent
patch (and there seem to be no problems in this area with files opened
with fmemopen).

Tested for x86_64.
This commit is contained in:
Joseph Myers
2025-01-28 19:38:27 +00:00
parent 1515f74fd8
commit 377e9733b5
3 changed files with 70 additions and 0 deletions

View File

@@ -800,6 +800,11 @@ _IO_new_file_sync (FILE *fp)
if (fp->_IO_write_ptr > fp->_IO_write_base)
if (_IO_do_flush(fp)) return EOF;
delta = fp->_IO_read_ptr - fp->_IO_read_end;
if (_IO_in_backup (fp))
{
_IO_switch_to_main_get_area (fp);
delta += fp->_IO_read_ptr - fp->_IO_read_end;
}
if (delta != 0)
{
off64_t new_pos = _IO_SYSSEEK (fp, delta, 1);