From 0dcc0b2f63051863187dc678964eb17761b1a820 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Tue, 28 Jan 2025 22:35:21 +0000 Subject: Fix fseek handling for mmap files after ungetc or fflush (bug 32529) As discussed in bug 32529, fseek fails on files opened for reading using mmap after ungetc. The implementation of fseek for such files has an offset computation that's also incorrect after fflush. A combined fix addresses both problems (with tests for both included as well) and it seems reasonable to consider them a single bug. Tested for x86_64. --- libio/fileops.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'libio') diff --git a/libio/fileops.c b/libio/fileops.c index e868d520b6..b00a5d24ab 100644 --- a/libio/fileops.c +++ b/libio/fileops.c @@ -1107,11 +1107,18 @@ _IO_file_seekoff_mmap (FILE *fp, off64_t offset, int dir, int mode) if (mode == 0) return fp->_offset - (fp->_IO_read_end - fp->_IO_read_ptr); + if (_IO_in_backup (fp)) + { + if (dir == _IO_seek_cur) + offset += fp->_IO_read_ptr - fp->_IO_read_end; + _IO_switch_to_main_get_area (fp); + } + switch (dir) { case _IO_seek_cur: /* Adjust for read-ahead (bytes is buffer). */ - offset += fp->_IO_read_ptr - fp->_IO_read_base; + offset += fp->_offset - (fp->_IO_read_end - fp->_IO_read_ptr); break; case _IO_seek_set: break; -- cgit v1.2.3