git: 4e0e01bf6511 - main - fflush: correct buffer handling in __sflush

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Tue, 07 Nov 2023 17:28:28 UTC
The branch main has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=4e0e01bf6511c28212d7dff94fe131a502e13026

commit 4e0e01bf6511c28212d7dff94fe131a502e13026
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2023-11-07 14:16:13 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2023-11-07 16:03:34 +0000

    fflush: correct buffer handling in __sflush
    
    Two additional stdio changes followed 86a16ada1ea6 and need to be
    reverted as part of the fflush fix.
    
    This reverts commit 6e13794fbe6e82c21365d0fd66769bf8b19c0197.
    This reverts commit bafaa70b6f9098d83d074968c8e6747ecec1e118.
    
    Fixes: d09a3bf72c0b ("fflush: correct buffer handling in __sflush")
    Reviewed by: markj
    Sponsored by: The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D42491
---
 lib/libc/stdio/fvwrite.c | 13 ++-----------
 lib/libc/stdio/wbuf.c    | 12 ++----------
 2 files changed, 4 insertions(+), 21 deletions(-)

diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c
index 63d17a8f8ffd..6162ca01aa63 100644
--- a/lib/libc/stdio/fvwrite.c
+++ b/lib/libc/stdio/fvwrite.c
@@ -51,7 +51,6 @@ int
 __sfvwrite(FILE *fp, struct __suio *uio)
 {
 	size_t len;
-	unsigned char *old_p;
 	char *p;
 	struct __siov *iov;
 	int w, s;
@@ -135,12 +134,8 @@ __sfvwrite(FILE *fp, struct __suio *uio)
 				COPY(w);
 				/* fp->_w -= w; */ /* unneeded */
 				fp->_p += w;
-				old_p = fp->_p;
-				if (__fflush(fp) == EOF) {
-					if (old_p == fp->_p)
-						fp->_p -= w;
+				if (__fflush(fp))
 					goto err;
-				}
 			} else if (len >= (w = fp->_bf._size)) {
 				/* write directly */
 				w = _swrite(fp, p, w);
@@ -179,12 +174,8 @@ __sfvwrite(FILE *fp, struct __suio *uio)
 				COPY(w);
 				/* fp->_w -= w; */
 				fp->_p += w;
-				old_p = fp->_p;
-				if (__fflush(fp) == EOF) {
-					if (old_p == fp->_p)
-						fp->_p -= w;
+				if (__fflush(fp))
 					goto err;
-				}
 			} else if (s >= (w = fp->_bf._size)) {
 				w = _swrite(fp, p, w);
 				if (w <= 0)
diff --git a/lib/libc/stdio/wbuf.c b/lib/libc/stdio/wbuf.c
index 7153350c99f1..9aab55a207cb 100644
--- a/lib/libc/stdio/wbuf.c
+++ b/lib/libc/stdio/wbuf.c
@@ -49,7 +49,6 @@ static char sccsid[] = "@(#)wbuf.c	8.1 (Berkeley) 6/4/93";
 int
 __swbuf(int c, FILE *fp)
 {
-	unsigned char *old_p;
 	int n;
 
 	/*
@@ -85,15 +84,8 @@ __swbuf(int c, FILE *fp)
 	}
 	fp->_w--;
 	*fp->_p++ = c;
-	old_p = fp->_p;
-	if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n')) {
-		if (__fflush(fp) != 0) {
-			if (fp->_p == old_p) {
-				fp->_p--;
-				fp->_w++;
-			}
+	if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n'))
+		if (__fflush(fp) != 0)
 			return (EOF);
-		}
-	}
 	return (c);
 }