From 2482ae433a4249495859343ae1fba408300f2c2e Mon Sep 17 00:00:00 2001 From: Siddhesh Poyarekar Date: Tue, 27 May 2014 13:54:19 +0530 Subject: Fix offset computation for append+ mode on switching from read (BZ #16724) The offset computation in write mode uses the fact that _IO_read_end is kept in sync with the external file offset. This however is not true when O_APPEND is in effect since switching to write mode ought to send the external file offset to the end of file without making the necessary adjustment to _IO_read_end. Hence in append mode, offset computation when writing should only consider the effect of unflushed writes, i.e. from _IO_write_base to _IO_write_ptr. The wiki has a detailed document that describes the rationale for offsets returned by ftell in various conditions: https://sourceware.org/glibc/wiki/File%20offsets%20in%20a%20stdio%20stream%20and%20ftell --- libio/wfileops.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'libio/wfileops.c') diff --git a/libio/wfileops.c b/libio/wfileops.c index 3199861b4d..f123add354 100644 --- a/libio/wfileops.c +++ b/libio/wfileops.c @@ -713,9 +713,16 @@ do_ftell_wide (_IO_FILE *fp) offset += outstop - out; } - /* _IO_read_end coincides with fp._offset, so the actual file - position is fp._offset - (_IO_read_end - new_write_ptr). */ - offset -= fp->_IO_read_end - fp->_IO_write_ptr; + /* We don't trust _IO_read_end to represent the current file offset + when writing in append mode because the value would have to be + shifted to the end of the file during a flush. Use the write base + instead, along with the new offset we got above when we did a seek + to the end of the file. */ + if (append_mode) + offset += fp->_IO_write_ptr - fp->_IO_write_base; + /* For all other modes, _IO_read_end represents the file offset. */ + else + offset += fp->_IO_write_ptr - fp->_IO_read_end; } } -- cgit 1.4.1