Bug 21735 - open_memstream is not fflushed with fflush(0)
Summary: open_memstream is not fflushed with fflush(0)
Status: RESOLVED WORKSFORME
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-07-07 14:57 UTC by Szabolcs Nagy
Modified: 2019-01-10 16:39 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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);
	fflush(0);
	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 -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=de895ddcd7fc45caeeeb0ae312311b8bd31d82c5

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:

  http://austingroupbugs.net/view.php?id=1156
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.