fflush.c.patch   [plain text]


--- fflush.c.bsdnew	2009-11-11 13:33:04.000000000 -0800
+++ fflush.c	2009-11-11 13:33:04.000000000 -0800
@@ -39,6 +39,7 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/f
 #include "namespace.h"
 #include <errno.h>
 #include <stdio.h>
+#include <string.h>
 #include "un-namespace.h"
 #include "libc_private.h"
 #include "local.h"
@@ -122,6 +123,14 @@ __sflush(FILE *fp)
 	for (; n > 0; n -= t, p += t) {
 		t = _swrite(fp, (char *)p, n);
 		if (t <= 0) {
+			/* 5340694: reset _p and _w on EAGAIN */
+			if (t < 0 && errno == EAGAIN) {
+				if (p > fp->_p) /* some was written */
+					memmove(fp->_p, p, n);
+				fp->_p += n;
+				if (!(fp->_flags & (__SLBF|__SNBF)))
+					fp->_w -= n;
+			}
 			fp->_flags |= __SERR;
 			return (EOF);
 		}