Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Sun, 18 Sep 2016 21:44:55 +0200
From: Denys Vlasenko <vda.linux@...glemail.com>
To: Rich Felker <dalias@...c.org>
Cc: Denys Vlasenko <vda.linux@...glemail.com>, musl <musl@...ts.openwall.com>
Subject: [PATCH] make fflush_unlocked(NULL) work

In glibc, fflush_unlocked(NULL) works.
Before this patch, musl was segfaulting.

Signed-off-by: Denys Vlasenko <vda.linux@...glemail.com>
CC: musl <musl@...ts.openwall.com>
---
 src/stdio/fflush.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/src/stdio/fflush.c b/src/stdio/fflush.c
index 3f462c8..06d7a56 100644
--- a/src/stdio/fflush.c
+++ b/src/stdio/fflush.c
@@ -2,20 +2,35 @@
 
 static int __fflush_unlocked(FILE *f)
 {
-	/* If writing, flush output */
-	if (f->wpos > f->wbase) {
-		f->write(f, 0, 0);
-		if (!f->wpos) return EOF;
+	int r;
+
+	if (f) {
+		/* If writing, flush output */
+		if (f->wpos > f->wbase) {
+			f->write(f, 0, 0);
+			if (!f->wpos) return EOF;
+		}
+
+		/* If reading, sync position, per POSIX */
+		if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR);
+
+		/* Clear read and write modes */
+		f->wpos = f->wbase = f->wend = 0;
+		f->rpos = f->rend = 0;
+
+		return 0;
 	}
 
-	/* If reading, sync position, per POSIX */
-	if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR);
+	/* fflush_unlocked(NULL) is supported by glibc, mimic that */
 
-	/* Clear read and write modes */
-	f->wpos = f->wbase = f->wend = 0;
-	f->rpos = f->rend = 0;
+	r = __stdout_used ? __fflush_unlocked(__stdout_used) : 0;
 
-	return 0;
+	for (f=*__ofl_lock(); f; f=f->next) {
+		if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
+	}
+	__ofl_unlock();
+
+	return r;
 }
 
 /* stdout.c will override this if linked */
-- 
2.9.2

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.