diff mbox series

[uclibc-ng-devel,2/2] stdio: make an open_memstream byte-oriented, as POSIX requires

Message ID 20260831080159.3552089-2-lordrasmus@gmail.com
State New
Headers show
Series [uclibc-ng-devel,1/2] stdio: report the position as an open_memstream's size, as POSIX requires | expand

Commit Message

Ramin Moussavi Aug. 31, 2026, 8:01 a.m. UTC
From: ramin <lordrasmus@gmail.com>

POSIX: "The stream associated with a call to open_memstream() shall be
byte-oriented."  fopencookie() leaves a new stream unoriented, so fwide(f, 0)
answered 0 instead of a negative value and the first wide operation could have
turned the stream wide.

__STDIO_STREAM_SET_NARROW() is a no-op when UCLIBC_HAS_WCHAR is off, where
there is no orientation to set, so no guard is needed.

fmemopen() is deliberately left alone: its POSIX page says nothing about
orientation.

Found by tst-fmemopen in uclibc-ng-test, which now walks the whole
open_memstream description.

Signed-off-by: Ramin Moussavi <lordrasmus@gmail.com>
---
 libc/stdio/open_memstream.c | 4 ++++
 1 file changed, 4 insertions(+)
diff mbox series

Patch

diff --git a/libc/stdio/open_memstream.c b/libc/stdio/open_memstream.c
index 7e8e2134f..716d8666e 100644
--- a/libc/stdio/open_memstream.c
+++ b/libc/stdio/open_memstream.c
@@ -188,6 +188,10 @@  FILE *open_memstream(char **bufloc, size_t *sizeloc)
 		 * nothing currently in the FILE's write buffer. */
 
 		if (fp != NULL) {
+			/* POSIX: "The stream associated with a call to
+			 * open_memstream() shall be byte-oriented."  fopencookie()
+			 * leaves it unoriented. */
+			__STDIO_STREAM_SET_NARROW(fp);
 			__STDIO_STREAM_VALIDATE(fp);
 			return fp;
 		}