Module Name:    src
Committed By:   christos
Date:           Fri Jul 16 12:34:10 UTC 2021

Modified Files:
        src/lib/libc/stdio: fvwrite.c

Log Message:
When fflush fails, adjust pointers and the io vectors. From RVP. Fixes
core-dump at cvs(1) exit(3).


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/lib/libc/stdio/fvwrite.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/stdio/fvwrite.c
diff -u src/lib/libc/stdio/fvwrite.c:1.27 src/lib/libc/stdio/fvwrite.c:1.28
--- src/lib/libc/stdio/fvwrite.c:1.27	Thu Jul  8 05:06:51 2021
+++ src/lib/libc/stdio/fvwrite.c	Fri Jul 16 08:34:10 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: fvwrite.c,v 1.27 2021/07/08 09:06:51 christos Exp $	*/
+/*	$NetBSD: fvwrite.c,v 1.28 2021/07/16 12:34:10 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)fvwrite.c	8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: fvwrite.c,v 1.27 2021/07/08 09:06:51 christos Exp $");
+__RCSID("$NetBSD: fvwrite.c,v 1.28 2021/07/16 12:34:10 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -51,6 +51,40 @@ __RCSID("$NetBSD: fvwrite.c,v 1.27 2021/
 #include "local.h"
 #include "fvwrite.h"
 
+static int
+flush_adj(FILE *fp, struct __suio *uio, struct __siov *iov, ssize_t w)
+{
+	int rc;
+
+	_DIAGASSERT(w >= 0);
+	_DIAGASSERT(fp->_w >= 0);
+
+	if ((rc = fflush(fp)) == 0)
+		return 0;
+
+	/*
+	 * If we have to return without writing the whole buffer,
+	 * adjust for how much fflush() has written for us.
+	 * `w' is the amt. of new user data just copied into our
+	 * internal buffer in _this_ fwrite() call.
+         */
+	if (fp->_w < w)	{
+		/* some new data was also written */
+		ssize_t i = w - fp->_w;
+
+		/* adjust amt. written */
+		uio->uio_resid -= i;
+		iov->iov_len -= i;
+	} else {
+		/* only old stuff was written */
+
+		/* adjust _p and _w so user can retry */
+		fp->_p -= w;
+		fp->_w += w;
+	}
+	return rc;
+}
+
 /*
  * Write some memory regions.  Return zero on success, EOF on error.
  *
@@ -102,10 +136,8 @@ __sfvwrite(FILE *fp, struct __suio *uio)
 	if (w <= 0) \
 		goto err
 #define FLUSH(nw) \
-	if (fflush(fp)) { \
-		fp->_p -= nw;	/* rewind unwritten */ \
-		goto err; \
-	}
+	if (flush_adj(fp, uio, iov - 1, nw)) \
+		goto err
 
 	if (fp->_flags & __SNBF) {
 		/*

Reply via email to