Follow @Openwall on Twitter for new release announcements and other news
[<prev] [<thread-prev] [day] [month] [year] [list]
Message-ID: <2c2d7f41-4ce5-4517-a619-f7c72f94d80b@gmail.com>
Date: Sat, 21 Nov 2026 20:11:05 +0100
From: Bartosz Brachaczek <b.brachaczek@...il.com>
To: musl@...ts.openwall.com
Subject: Re: musl libc 1.2.5: fmemopen(NULL, 0, "w+") writes past a
 zero-capacity buffer

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).

Bartosz

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.