[v3,2/2] libio: Ignore doallocate for open_memstream and open_wmemstream [BZ #34019]

Message ID 20260506102327.24801-2-gaoxiang@kylinos.cn (mailing list archive)
State New
Headers
Series [v3,1/2] libio: Ignore setbuf for open_memstream and open_wmemstream [BZ #34019] |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686

Commit Message

Gao Xiang May 6, 2026, 10:23 a.m. UTC
  From: Xiang Gao <gaoxiang@kylinos.cn>

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>
---
 libio/Makefile                            |  1 +
 libio/libioP.h                            |  3 ++
 libio/memstream.c                         |  9 ++++
 libio/tst-wmemstream-setvbuf-doallocate.c | 64 +++++++++++++++++++++++
 libio/vtables.c                           |  6 ++-
 libio/wmemstream.c                        |  9 ++++
 6 files changed, 90 insertions(+), 2 deletions(-)
 create mode 100644 libio/tst-wmemstream-setvbuf-doallocate.c
  

Comments

Andreas Schwab May 6, 2026, 10:38 a.m. UTC | #1
On Mai 06 2026, Gao Xiang wrote:

> diff --git a/libio/tst-wmemstream-setvbuf-doallocate.c b/libio/tst-wmemstream-setvbuf-doallocate.c
> new file mode 100644
> index 0000000000..595a716db5
> --- /dev/null
> +++ b/libio/tst-wmemstream-setvbuf-doallocate.c
> @@ -0,0 +1,64 @@
> +/* Test setvbuf on open_wmenstream, 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 <stdio.h>
> +#include <stdlib.h>
> +#include <wchar.h>
> +#include <support/check.h>
> +
> +static int
> +do_test (void)
> +{
> +  /* Regression test for setvbuf doallocate on open_wmemstream.
> +     This test cover the _IO_setvbuf path for:
> +
> +     setvbuf (stream, NULL, _IOFBF, 0)
> +
> +     This path may call _IO_DOALLOCATE and return without invoking
> +     the stream setbuf hook. For open_wmemstream, the generic wide
> +     doallocate hook must not replace the growable  result buffer.  */
> +
> +  wchar_t *wbuf = NULL;
> +  size_t wlen = 0;
> +  FILE *fp = open_wmemstream (&wbuf, &wlen);

If you base the test on libio/tst-memstream.h you can test both narrow
and wide streams with the same test.
  

Patch

diff --git a/libio/Makefile b/libio/Makefile
index 584fcdb14d..b3c2d818b3 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -140,6 +140,7 @@  tests = \
   tst-wfile-sync \
   tst-wfiledoallocate-static \
   tst-widetext \
+  tst-wmemstream-setvbuf-doallocate \
   tst-wmemstream1 \
   tst-wmemstream2 \
   tst-wmemstream3 \
diff --git a/libio/libioP.h b/libio/libioP.h
index 17c0b6e76d..fa547a4a29 100644
--- a/libio/libioP.h
+++ b/libio/libioP.h
@@ -742,10 +742,13 @@  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_;
diff --git a/libio/memstream.c b/libio/memstream.c
index a5f909cf64..918a2d6015 100644
--- a/libio/memstream.c
+++ b/libio/memstream.c
@@ -121,3 +121,12 @@  _IO_mem_setbuf (FILE *fp, char *p, ssize_t len)
   (void) len;
   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.  */
+  (void) fp;
+  return 1;
+}
diff --git a/libio/tst-wmemstream-setvbuf-doallocate.c b/libio/tst-wmemstream-setvbuf-doallocate.c
new file mode 100644
index 0000000000..595a716db5
--- /dev/null
+++ b/libio/tst-wmemstream-setvbuf-doallocate.c
@@ -0,0 +1,64 @@ 
+/* Test setvbuf on open_wmenstream, 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 <stdio.h>
+#include <stdlib.h>
+#include <wchar.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+  /* Regression test for setvbuf doallocate on open_wmemstream.
+     This test cover the _IO_setvbuf path for:
+
+     setvbuf (stream, NULL, _IOFBF, 0)
+
+     This path may call _IO_DOALLOCATE and return without invoking
+     the stream setbuf hook. For open_wmemstream, the generic wide
+     doallocate hook must not replace the growable  result buffer.  */
+
+  wchar_t *wbuf = NULL;
+  size_t wlen = 0;
+  FILE *fp = open_wmemstream (&wbuf, &wlen);
+
+  TEST_VERIFY_EXIT (fp != NULL);
+
+  TEST_COMPARE (setvbuf (fp, NULL, _IOFBF, 0), 0);
+
+  TEST_COMPARE (fputwc (L'A', fp), L'A');
+  TEST_COMPARE (fflush (fp), 0);
+  TEST_COMPARE (wlen, 1);
+  TEST_VERIFY (wbuf != NULL);
+  TEST_VERIFY (wbuf[0] == L'A');
+  TEST_VERIFY (wbuf[1] == L'\0');
+
+  TEST_COMPARE (fputwc (L'B', fp), L'B');
+  TEST_COMPARE (fclose (fp), 0);
+  TEST_COMPARE (wlen, 2);
+  TEST_VERIFY (wbuf != NULL);
+  TEST_VERIFY (wbuf[0] == L'A');
+  TEST_VERIFY (wbuf[1] == L'B');
+  TEST_VERIFY (wbuf[2] == L'\0');
+
+  free (wbuf);
+
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/libio/vtables.c b/libio/vtables.c
index ca9f1b2dc4..ba3f9566f8 100644
--- a/libio/vtables.c
+++ b/libio/vtables.c
@@ -79,10 +79,12 @@ 
 # 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),
diff --git a/libio/wmemstream.c b/libio/wmemstream.c
index 8f37a6efb8..498f5420e1 100644
--- a/libio/wmemstream.c
+++ b/libio/wmemstream.c
@@ -127,3 +127,12 @@  _IO_wmem_setbuf (FILE *fp, char *p, ssize_t len)
   (void) len;
   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.  */
+  (void) fp;
+  return 1;
+}