]> xenbits.xensource.com Git - people/julieng/freebsd.git/commitdiff
Don't seek to the end if write buffer is empty (in append modes).
authorache <ache@FreeBSD.org>
Sun, 1 Nov 2015 06:15:14 +0000 (06:15 +0000)
committerache <ache@FreeBSD.org>
Sun, 1 Nov 2015 06:15:14 +0000 (06:15 +0000)
PR:             204156
MFC after:      1 week

lib/libc/stdio/ftell.c

index abb6da11966c908ec9c2322536441d6d62573c3e..8cf94ee39c91591a86c2c5fb4e5c2b54cb47da22 100644 (file)
@@ -119,7 +119,18 @@ _ftello(FILE *fp, fpos_t *offset)
                if (HASUB(fp))
                        pos -= fp->_r;  /* Can be negative at this point. */
        } else if ((fp->_flags & __SWR) && fp->_p != NULL) {
-               if ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP)) {
+               /*
+                * Writing.  Any buffered characters cause the
+                * position to be greater than that in the
+                * underlying object.
+                */
+               n = fp->_p - fp->_bf._base;
+               if (pos > OFF_MAX - n) {
+                       errno = EOVERFLOW;
+                       return (1);
+               }
+               if (n > 0 &&
+                   ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP))) {
                        int serrno = errno;
 
                        errno = 0;
@@ -137,16 +148,6 @@ _ftello(FILE *fp, fpos_t *offset)
                        }
                        errno = serrno;
                }
-               /*
-                * Writing.  Any buffered characters cause the
-                * position to be greater than that in the
-                * underlying object.
-                */
-               n = fp->_p - fp->_bf._base;
-               if (pos > OFF_MAX - n) {
-                       errno = EOVERFLOW;
-                       return (1);
-               }
                pos += n;
        }
        *offset = pos;