public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: newlib-cvs@sourceware.org
Subject: [newlib-cygwin] newlib: ftello{64}: Handle appending stream without fflushing
Date: Mon, 18 Dec 2017 19:18:00 -0000	[thread overview]
Message-ID: <20171218191826.82911.qmail@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=6e5b39940ae95d098fb89cf80d48f6e03d3833bf

commit 6e5b39940ae95d098fb89cf80d48f6e03d3833bf
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Mon Dec 18 20:17:51 2017 +0100

    newlib: ftello{64}: Handle appending stream without fflushing
    
    Neither upstream FreeBSD nor glibc ever call fflush from ftell
    and friends.  In border cases it has the tendency to return
    wrong or unexpected values, for instance on block devices.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 newlib/libc/stdio/ftello.c     | 20 +++++++++++++-------
 newlib/libc/stdio64/ftello64.c | 20 +++++++++++++-------
 2 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/newlib/libc/stdio/ftello.c b/newlib/libc/stdio/ftello.c
index 0a9bb7b..90c4d32 100644
--- a/newlib/libc/stdio/ftello.c
+++ b/newlib/libc/stdio/ftello.c
@@ -102,13 +102,19 @@ _DEFUN(_ftello_r, (ptr, fp),
       return (_off_t) -1;
     }
 
-  /* Find offset of underlying I/O object, then adjust for buffered
-     bytes.  Flush a write stream, since the offset may be altered if
-     the stream is appending.  Do not flush a read stream, since we
-     must not lose the ungetc buffer.  */
-  if (fp->_flags & __SWR)
-    _fflush_r (ptr, fp);
-  if (fp->_flags & __SOFF)
+  /* Find offset of underlying I/O object, then adjust for buffered bytes. */
+  if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
+      fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
+      (fp->_flags & __SAPP))
+    {
+      pos = fp->_seek (ptr, fp->_cookie, (_fpos_t) 0, SEEK_END);
+      if (pos == (_fpos_t) -1)
+	{
+          _newlib_flockfile_exit (fp);
+          return (_off_t) -1;
+	}
+    }
+  else if (fp->_flags & __SOFF)
     pos = fp->_offset;
   else
     {
diff --git a/newlib/libc/stdio64/ftello64.c b/newlib/libc/stdio64/ftello64.c
index 9aca231..2441d4c 100644
--- a/newlib/libc/stdio64/ftello64.c
+++ b/newlib/libc/stdio64/ftello64.c
@@ -99,13 +99,19 @@ _DEFUN (_ftello64_r, (ptr, fp),
       return (_off64_t) -1;
     }
 
-  /* Find offset of underlying I/O object, then adjust for buffered
-     bytes.  Flush a write stream, since the offset may be altered if
-     the stream is appending.  Do not flush a read stream, since we
-     must not lose the ungetc buffer.  */
-  if (fp->_flags & __SWR)
-    _fflush_r (ptr, fp);
-  if (fp->_flags & __SOFF)
+  /* Find offset of underlying I/O object, then adjust for buffered bytes. */
+  if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
+      fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
+      (fp->_flags & __SAPP))
+    {
+      pos = fp->_seek64 (ptr, fp->_cookie, (_fpos64_t) 0, SEEK_END);
+      if (pos == (_fpos64_t) -1)
+	{
+          _newlib_flockfile_exit (fp);
+          return (_off64_t) -1;
+	}
+    }
+  else if (fp->_flags & __SOFF)
     pos = fp->_offset;
   else
     {


                 reply	other threads:[~2017-12-18 19:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171218191826.82911.qmail@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=newlib-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).