From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10496 Path: news.gmane.org!.POSTED!not-for-mail From: Denys Vlasenko Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] make fflush_unlocked(NULL) work Date: Sun, 18 Sep 2016 21:44:55 +0200 Message-ID: <20160918194455.21981-1-vda.linux@googlemail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1474227925 22234 195.159.176.226 (18 Sep 2016 19:45:25 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 18 Sep 2016 19:45:25 +0000 (UTC) Cc: Denys Vlasenko , musl To: Rich Felker Original-X-From: musl-return-10509-gllmg-musl=m.gmane.org@lists.openwall.com Sun Sep 18 21:45:21 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1bli1Z-0004NZ-EX for gllmg-musl@m.gmane.org; Sun, 18 Sep 2016 21:45:13 +0200 Original-Received: (qmail 26030 invoked by uid 550); 18 Sep 2016 19:45:13 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 26009 invoked from network); 18 Sep 2016 19:45:12 -0000 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Sun, 18 Sep 2016 19:45:00 +0000 (UTC) Xref: news.gmane.org gmane.linux.lib.musl.general:10496 Archived-At: In glibc, fflush_unlocked(NULL) works. Before this patch, musl was segfaulting. Signed-off-by: Denys Vlasenko CC: musl --- 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