Bug 21735

Summary: open_memstream is not fflushed with fflush(0)
Product: glibc Reporter: Szabolcs Nagy <nszabolcs>
Component: stdioAssignee: Not yet assigned to anyone <unassigned>
Severity: normal CC: fweimer
Priority: P2 Flags: fweimer: security-
Version: unspecified   
Target Milestone: ---   
See Also: http://austingroupbugs.net/view.php?id=1156
Host: Target:
Build: Last reconfirmed:

Description Szabolcs Nagy 2017-07-07 14:57:48 UTC
open_memstream is not exempt from the rule that fflush(0)
should fflush all open streams.

posix specifies the state of the buffer and size after
fflush on a stream opened by open_memstream and glibc
is not conform to it, e.g the following test fails:

#include <stdio.h>

int main()
	char *buf = 0;
	size_t n = 0;
	FILE *f = open_memstream(&buf, &n);
	putc('x', f);
	return n != 1;
Comment 1 Sourceware Commits 2017-07-14 15:11:08 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  de895ddcd7fc45caeeeb0ae312311b8bd31d82c5 (commit)
      from  8bd8c052e765d626fae1424e2739d009a185a6ba (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------

commit de895ddcd7fc45caeeeb0ae312311b8bd31d82c5
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Wed Jul 12 10:53:13 2017 +0100

    Disable single thread optimization for open_memstream
    Single thread optimization is valid if at thread creation time the
    optimization can be disabled.  This is in principle true for all
    stream objects that user code can access (and thus needs locking),
    using the same internal list as fflush(0) uses.  However in glibc
    open_memstream is not on that list (BZ 21735) so the optimization
    has to be disabled.
    	* libio/memstream.c (__open_memstream): Set _IO_FLAGS2_NEED_LOCK.
    	* libio/wmemstream.c (open_wmemstream): Likewise.
    	* nptl/tst-memstream.c: New.


Summary of changes:
 ChangeLog            |    7 +++
 libio/memstream.c    |    3 +
 libio/wmemstream.c   |    3 +
 nptl/Makefile        |    2 +-
 nptl/tst-memstream.c |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 115 insertions(+), 1 deletions(-)
 create mode 100644 nptl/tst-memstream.c
Comment 2 Florian Weimer 2017-07-20 08:13:54 UTC
I filed an issue with the Austin Group, requesting clarification:

Comment 3 Florian Weimer 2019-01-10 16:39:19 UTC
If I interpret the recent update to the Austin Group issue correctly, an upcoming POSIX version will no longer require that fflush (NULL) flushes open_memstream streams.  This means that the current glibc behavior is acceptable.