Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20261122214056.GO5716@brightrain.aerifal.cx>
Date: Sun, 22 Nov 2026 16:40:56 -0500
From: Rich Felker <dalias@...c.org>
To: Bartosz Brachaczek <b.brachaczek@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: musl libc 1.2.5: fmemopen(NULL, 0, "w+") writes past a
 zero-capacity buffer

On Sat, Nov 21, 2026 at 08:11:05PM +0100, Bartosz Brachaczek wrote:
> On 11/19/2026 3:16 PM, Rich Felker wrote:
> > I belive the following fixes it:
> > 
> > diff --git a/src/stdio/fmemopen.c b/src/stdio/fmemopen.c
> > index 38ba9f85..bf2ce57f 100644
> > --- a/src/stdio/fmemopen.c
> > +++ b/src/stdio/fmemopen.c
> > @@ -118,7 +118,7 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
> >   	if (!plus) f->f.flags = (*mode == 'r') ? F_NOWR : F_NORD;
> >   	if (*mode == 'r') f->c.len = size;
> >   	else if (*mode == 'a') f->c.len = f->c.pos = strnlen(buf, size);
> > -	else if (plus) *f->c.buf = 0;
> > +	else if (plus && size) *f->c.buf = 0;
> >   	f->f.read = mread;
> >   	f->f.write = mwrite;
> 
> By the way, POSIX.1-2024 says:
> 
> "If the mode argument begins with 'w' and max_size is not zero, the buffer
> contents shall be truncated by writing a null byte at the beginning."
> 
> whereas POSIX.1-2017 mandated truncation only for w+. This is the result of
> Austin Group defect 657, which is also the source of the "or for writing
> only" change that is already implemented since
> bb4b4102461acfbf77da759ca3a6deb555f3da3b (whose commit message, I think,
> wrongly attributes the previous behavior to a misreading, while in fact the
> requirement only changed in POSIX.1-2024).

I think you're right, but this history is such a mess and it's giving
me a headache trawling through it. What I'm gathering, and it reflects
my high-level memory of what happened, is that the original POSIX spec
was just wrong and contrary to existing practice, and the new one has
fixed that.

Rich

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.