From be349d7042de84c3c5157a5c1fbcad580aed33e1 Mon Sep 17 00:00:00 2001 From: Siddhesh Poyarekar Date: Thu, 4 Dec 2014 08:08:37 +0530 Subject: ftell: seek to end only when there are unflushed bytes (BZ #17647) Currently we seek to end of file if there are unflushed writes or the stream is in write mode, to get the current offset for writing in append mode, which is the end of file. The latter case (i.e. stream is in write mode, but no unflushed writes) is unnecessary since it will only happen when the stream has just been flushed, in which case the recorded offset ought to be reliable. Removing that case lets ftell give the correct offset when it follows an ftruncate. The latter truncates the file, but does not change the file position, due to which it is permissible to call ftell without an intervening fseek call. Tested on x86_64 to verify that the added test case fails without the patch and succeeds with it, and that there are no additional regressions due to it. [BZ #17647] * libio/fileops.c (do_ftell): Seek only when there are unflushed writes. * libio/wfileops.c (do_ftell_wide): Likewise. * libio/tst-ftell-active-handler.c (do_ftruncate_test): New test case. (do_one_test): Call it. --- libio/wfileops.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'libio/wfileops.c') diff --git a/libio/wfileops.c b/libio/wfileops.c index 6a088b1..71281c1 100644 --- a/libio/wfileops.c +++ b/libio/wfileops.c @@ -626,16 +626,15 @@ do_ftell_wide (_IO_FILE *fp) const wchar_t *wide_read_base; const wchar_t *wide_read_ptr; const wchar_t *wide_read_end; - bool was_writing = ((fp->_wide_data->_IO_write_ptr - > fp->_wide_data->_IO_write_base) - || _IO_in_put_mode (fp)); + bool unflushed_writes = (fp->_wide_data->_IO_write_ptr + > fp->_wide_data->_IO_write_base); bool append_mode = (fp->_flags & _IO_IS_APPENDING) == _IO_IS_APPENDING; /* When we have unflushed writes in append mode, seek to the end of the file and record that offset. This is the only time we change the file stream state and it is safe since the file handle is active. */ - if (was_writing && append_mode) + if (unflushed_writes && append_mode) { result = _IO_SYSSEEK (fp, 0, _IO_seek_end); if (result == _IO_pos_BAD) @@ -674,7 +673,7 @@ do_ftell_wide (_IO_FILE *fp) struct _IO_codecvt *cv = fp->_codecvt; int clen = (*cv->__codecvt_do_encoding) (cv); - if (!was_writing) + if (!unflushed_writes) { if (clen > 0) { -- cgit v1.1