]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
libio: Ignore doallocate for open_memstream and open_wmemstream [BZ #34019]
authorXiang Gao <gaoxiang@kylinos.cn>
Fri, 8 May 2026 06:04:10 +0000 (14:04 +0800)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 20 May 2026 20:22:19 +0000 (17:22 -0300)
setvbuf (stream, NULL, _IOFBF, 0) takes a special path in
_IO_setvbuf: if the byte-oriented buffer base is NULL, it calls
_IO_DOALLOCATE and returns without invoking the stream setbuf hook.

For open_wmemstream, the byte-oriented buffer base is NULL although
the wide result buffer has already been initialized in _wide_data.
As a result, this path calls _IO_wdefault_doallocate, which may
replace the wide buffer managed by open_wmemstream.

Install an open_wmemstream-specific doallocate hook that leaves
the growable result buffer unchanged. Add a regression test for this
path.

Install a narrow memstream doallocate hook as well. It keeps both
memstream vtables consistent (generic stdio allocation must not
replace the growable result buffer).

Signed-off-by: Xiang Gao <gaoxiang@kylinos.cn>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
libio/Makefile
libio/libioP.h
libio/memstream.c
libio/tst-memstream-setvbuf-doallocate.c [new file with mode: 0644]
libio/tst-wmemstream-setvbuf-doallocate.c [new file with mode: 0644]
libio/vtables.c
libio/wmemstream.c

index 916542f23be3df56eb46a5a0c06c143a0c9bde52..616107ee105b0698866e12aa730391c70a819c77 100644 (file)
@@ -120,6 +120,7 @@ tests = \
   tst-fwrite-error \
   tst-getdelim \
   tst-memstream-setbuf \
+  tst-memstream-setvbuf-doallocate \
   tst-memstream1 \
   tst-memstream2 \
   tst-memstream3 \
@@ -144,6 +145,7 @@ tests = \
   tst-wfiledoallocate-static \
   tst-widetext \
   tst-wmemstream-setbuf \
+  tst-wmemstream-setvbuf-doallocate \
   tst-wmemstream1 \
   tst-wmemstream2 \
   tst-wmemstream3 \
index fd423071160589d2a267f186d4cdec145ecda16c..78e8ee6835f20c672b86232f23aad845fb2d0f96 100644 (file)
@@ -743,10 +743,12 @@ extern int _IO_mem_sync (FILE* fp) __THROW attribute_hidden;
 extern void _IO_mem_finish (FILE* fp, int) __THROW attribute_hidden;
 extern FILE *_IO_mem_setbuf (FILE *fp, char *buf, ssize_t size)
   __THROW attribute_hidden;
+extern int _IO_mem_doallocate (FILE *fp) __THROW attribute_hidden;
 extern int _IO_wmem_sync (FILE* fp) __THROW attribute_hidden;
 extern void _IO_wmem_finish (FILE* fp, int) __THROW attribute_hidden;
 extern FILE *_IO_wmem_setbuf (FILE *fp, char *buf, ssize_t size)
   __THROW attribute_hidden;
+extern int _IO_wmem_doallocate (FILE *fp) __THROW attribute_hidden;
 
 /* Other strfile functions */
 struct _IO_strfile_;
index 69b400d9288855efb8d4a30761569368eb560573..1af7224ab5b6715b8113262cc0a105bdda182f07 100644 (file)
@@ -120,3 +120,12 @@ _IO_mem_setbuf (FILE *fp, char *p, ssize_t len)
   /* Memstream manage a growable buffer internally.  */
   return fp;
 }
+
+
+int
+_IO_mem_doallocate (FILE *fp)
+{
+  /* Memstream manage a growable buffer internally.  The doallocate
+     hook must not replace it with a generic stdio buffer.  */
+  return 1;
+}
diff --git a/libio/tst-memstream-setvbuf-doallocate.c b/libio/tst-memstream-setvbuf-doallocate.c
new file mode 100644 (file)
index 0000000..aa6f86f
--- /dev/null
@@ -0,0 +1,61 @@
+/* Test setvbuf on open_memstream, BZ #34019.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <libio/tst-memstream.h>
+
+static int
+do_test (void)
+{
+  /* Regression test for setvbuf doallocate on open_memstream and
+     open_wmemstream.  This test covers the _IO_setvbuf path for:
+
+     setvbuf (stream, NULL, _IOFBF, 0)
+
+     This path may call _IO_DOALLOCATE and return without invoking
+     the stream setbuf hook.  The generic doallocate hook must not
+     replace the growable result buffer.  */
+
+  CHAR_T *buf = NULL;
+  size_t len = 0;
+  FILE *fp = OPEN_MEMSTREAM (&buf, &len);
+
+  TEST_VERIFY_EXIT (fp != NULL);
+
+  TEST_COMPARE (setvbuf (fp, NULL, _IOFBF, 0), 0);
+
+  TEST_COMPARE (FPUTC (W('A'), fp), W('A'));
+  TEST_COMPARE (fflush (fp), 0);
+  TEST_COMPARE (len, 1);
+  TEST_VERIFY_EXIT (buf != NULL);
+  TEST_VERIFY (buf[0] == W('A'));
+  TEST_VERIFY (buf[1] == W('\0'));
+
+  TEST_COMPARE (FPUTC (W('B'), fp), W('B'));
+  TEST_COMPARE (fclose (fp), 0);
+  TEST_COMPARE (len, 2);
+  TEST_VERIFY_EXIT (buf != NULL);
+  TEST_VERIFY (buf[0] == W('A'));
+  TEST_VERIFY (buf[1] == W('B'));
+  TEST_VERIFY (buf[2] == W('\0'));
+
+  free (buf);
+
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/libio/tst-wmemstream-setvbuf-doallocate.c b/libio/tst-wmemstream-setvbuf-doallocate.c
new file mode 100644 (file)
index 0000000..857239a
--- /dev/null
@@ -0,0 +1,20 @@
+/* Test setvbuf on open_wmemstream, BZ #34019.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#define TEST_WCHAR
+#include <libio/tst-memstream-setvbuf-doallocate.c>
index ca9f1b2dc49a08773a781664762b51b06ec871c2..ba3f9566f87f7671b17edf76a4b7996ec8bb7dbc 100644 (file)
 # pragma weak _IO_mem_finish
 # pragma weak _IO_mem_setbuf
 # pragma weak _IO_mem_sync
+# pragma weak _IO_mem_doallocate
 
 # pragma weak _IO_wmem_finish
 # pragma weak _IO_wmem_setbuf
 # pragma weak _IO_wmem_sync
+# pragma weak _IO_wmem_doallocate
 
 # pragma weak __printf_buffer_as_file_overflow
 # pragma weak __printf_buffer_as_file_xsputn
@@ -338,7 +340,7 @@ const struct _IO_jump_t __io_vtables[] attribute_relro =
     JUMP_INIT (seekpos, _IO_default_seekpos),
     JUMP_INIT (setbuf, _IO_mem_setbuf),
     JUMP_INIT (sync, _IO_mem_sync),
-    JUMP_INIT (doallocate, _IO_default_doallocate),
+    JUMP_INIT (doallocate, _IO_mem_doallocate),
     JUMP_INIT (read, _IO_default_read),
     JUMP_INIT (write, _IO_default_write),
     JUMP_INIT (seek, _IO_default_seek),
@@ -361,7 +363,7 @@ const struct _IO_jump_t __io_vtables[] attribute_relro =
     JUMP_INIT (seekpos, _IO_default_seekpos),
     JUMP_INIT (setbuf, _IO_wmem_setbuf),
     JUMP_INIT (sync, _IO_wmem_sync),
-    JUMP_INIT (doallocate, _IO_wdefault_doallocate),
+    JUMP_INIT (doallocate, _IO_wmem_doallocate),
     JUMP_INIT (read, _IO_default_read),
     JUMP_INIT (write, _IO_default_write),
     JUMP_INIT (seek, _IO_default_seek),
index cdee2a4151708e43d73389d16f2e04d2d9bc7f33..dffbb4a8085684360371b6a1b4f5b971a20aefae 100644 (file)
@@ -125,3 +125,12 @@ _IO_wmem_setbuf (FILE *fp, char *p, ssize_t len)
   /* Wmemstreams manage a growable buffer internally.  */
   return fp;
 }
+
+
+int
+_IO_wmem_doallocate (FILE *fp)
+{
+  /* Wmemstreams manage a growable buffer internally.  The doallocate
+     hook must not replace it with a generic stdio buffer.  */
+  return 1;
+}