[PATCH v2] libio: Fix fmemopen_write on appending condition

Rocket Ma marocketbd@gmail.com
Fri Apr 17 16:12:50 GMT 2026


> In POSIX note: Note that buf will not be null terminated if max_size bytes are
> written to the memory stream. Applications wanting to guarantee that the buffer
> will be null terminated need to call fmemopen() with max_size set to one byte
> smaller than the actual size of buf and set buf[max_size] to a null byte.
>
> So I think the sentence you mention means `assert (pos <= max_size)`,
> i.e., fwrite may writes at maximum of `max_size` bytes, and
> `buf[max_size] = '\0'`.
>
> I don't think your patch, adding a specific size of allocated buffer
> function, could work properly. What if the user setbuf manually? That
> may introduce inconsistent behavior back again.
>
> In my opinion, since we are manipulating memory, not interacting with kernel,
> we should directly set buf to NULL, so fwrite could pass write request to
> fmemopen_write directly, and it won't hurt much performance. We just reject
> any setbuf requests with errno, for example, EINVAL. If we do this,
> fflush/fclose will never fail, and user will know immediately if the
> space is not enough to write, it will never confuse user.

Ping


More information about the Libc-alpha mailing list