git: 2a92854d81a1 - stable/13 - libc/stdio: only roll FILE state back on EINTR

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 09 Sep 2022 17:02:25 UTC
The branch stable/13 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=2a92854d81a10f02a8e244fe0ea449104bda5400

commit 2a92854d81a10f02a8e244fe0ea449104bda5400
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-09-02 13:39:38 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-09-09 17:01:53 +0000

    libc/stdio: only roll FILE state back on EINTR
    
    PR:     266171
    
    (cherry picked from commit 44cf1e5eb470380442fa8e240e213a71b8fe81d4)
---
 lib/libc/stdio/fflush.c  | 3 ++-
 lib/libc/stdio/fvwrite.c | 5 +++--
 lib/libc/stdio/wbuf.c    | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c
index decc974907f4..f59565abd369 100644
--- a/lib/libc/stdio/fflush.c
+++ b/lib/libc/stdio/fflush.c
@@ -136,7 +136,8 @@ __sflush(FILE *fp)
 				fp->_p += n;
 				if ((fp->_flags & (__SLBF | __SNBF)) == 0)
 					fp->_w -= n;
-			} else if (p == fp->_p) { /* cond. to handle setvbuf */
+			/* conditional to handle setvbuf */
+			} else if (p == fp->_p && errno == EINTR) {
 				fp->_p = old_p;
 				fp->_w = old_w;
 			}
diff --git a/lib/libc/stdio/fvwrite.c b/lib/libc/stdio/fvwrite.c
index 2a161859afa9..b1b363e6f80d 100644
--- a/lib/libc/stdio/fvwrite.c
+++ b/lib/libc/stdio/fvwrite.c
@@ -38,6 +38,7 @@ static char sccsid[] = "@(#)fvwrite.c	8.1 (Berkeley) 6/4/93";
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -140,7 +141,7 @@ __sfvwrite(FILE *fp, struct __suio *uio)
 				fp->_p += w;
 				old_p = fp->_p;
 				if (__fflush(fp) == EOF) {
-					if (old_p == fp->_p)
+					if (old_p == fp->_p && errno == EINTR)
 						fp->_p -= w;
 					goto err;
 				}
@@ -184,7 +185,7 @@ __sfvwrite(FILE *fp, struct __suio *uio)
 				fp->_p += w;
 				old_p = fp->_p;
 				if (__fflush(fp) == EOF) {
-					if (old_p == fp->_p)
+					if (old_p == fp->_p && errno == EINTR)
 						fp->_p -= w;
 					goto err;
 				}
diff --git a/lib/libc/stdio/wbuf.c b/lib/libc/stdio/wbuf.c
index d389cdab0353..666bbf87aadd 100644
--- a/lib/libc/stdio/wbuf.c
+++ b/lib/libc/stdio/wbuf.c
@@ -91,7 +91,7 @@ __swbuf(int c, FILE *fp)
 	old_p = fp->_p;
 	if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n')) {
 		if (__fflush(fp) != 0) {
-			if (fp->_p == old_p) {
+			if (fp->_p == old_p && errno == EINTR) {
 				fp->_p--;
 				fp->_w++;
 			}