This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH v2] libio: Fix open_memstream fflush (NULL) (BZ#21735)


POSIX specifies that the state and size after a fflush on a stream
opened by open_memstream should be updated and current implementation
does not act accordingly on a fflush (NULL) (meant to act on all opened
streams).  This patch fixes it for open_{w}memstream.

Checked on x86_64-linux-gnu.

	[BZ #21735]
	* libio/memstream.c (_IO_mem_overflow): New function.
	(_IO_mem_jumps): User _IO_mem_overflow instead of _IO_str_overflow.
	(__open_memstream): Call _IO_link_in on the internal FILE object.
	* libio/wmemstream.c (_IO_wmem_overflow): New function.
	(_IO_wmem_jumps): User _IO_wmem_overflow instead of _IO_wstr_overflow.
	(__open_wmemstream): Call _IO_link_in on the internal FILE object.
	* libio/Makefile (test): Add tst-memstream4 and tst-wmemstream4.
	* libio/tst-memstream4.c: New file.
	* libio/tst-wmemstream4.c: Likewise.
	* libio/tst-memstream.h: Likewise.
---
 ChangeLog               | 12 ++++++
 libio/Makefile          |  4 +-
 libio/memstream.c       | 21 +++++++++--
 libio/tst-memstream.h   | 66 +++++++++++++++++++++++++++++++++
 libio/tst-memstream4.c  | 99 +++++++++++++++++++++++++++++++++++++++++++++++++
 libio/tst-wmemstream4.c | 20 ++++++++++
 libio/wmemstream.c      | 22 +++++++++--
 7 files changed, 234 insertions(+), 10 deletions(-)
 create mode 100644 libio/tst-memstream.h
 create mode 100644 libio/tst-memstream4.c
 create mode 100644 libio/tst-wmemstream4.c

diff --git a/libio/Makefile b/libio/Makefile
index 9d09bd8..492532f 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -57,8 +57,8 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
 	tst-mmap-eofsync tst-mmap-fflushsync bug-mmap-fflush \
 	tst-mmap2-eofsync tst-mmap-offend bug-fopena+ bug-wfflush \
 	bug-ungetc2 bug-ftell bug-ungetc3 bug-ungetc4 tst-fopenloc2 \
-	tst-memstream1 tst-memstream2 tst-memstream3 \
-	tst-wmemstream1 tst-wmemstream2 tst-wmemstream3 \
+	tst-memstream1 tst-memstream2 tst-memstream3 tst-memstream4 \
+	tst-wmemstream1 tst-wmemstream2 tst-wmemstream3 tst-wmemstream4 \
 	bug-memstream1 bug-wmemstream1 \
 	tst-setvbuf1 tst-popen1 tst-fgetwc bug-wsetpos tst-fseek \
 	tst-fwrite-error tst-ftell-partial-wide tst-ftell-active-handler \
diff --git a/libio/memstream.c b/libio/memstream.c
index e391efd..67ecc09 100644
--- a/libio/memstream.c
+++ b/libio/memstream.c
@@ -31,13 +31,14 @@ struct _IO_FILE_memstream
 
 static int _IO_mem_sync (_IO_FILE* fp) __THROW;
 static void _IO_mem_finish (_IO_FILE* fp, int) __THROW;
+static int _IO_mem_overflow (_IO_FILE *fp, int c) __THROW;
 
 
 static const struct _IO_jump_t _IO_mem_jumps libio_vtable =
 {
   JUMP_INIT_DUMMY,
   JUMP_INIT (finish, _IO_mem_finish),
-  JUMP_INIT (overflow, _IO_str_overflow),
+  JUMP_INIT (overflow, _IO_mem_overflow),
   JUMP_INIT (underflow, _IO_str_underflow),
   JUMP_INIT (uflow, _IO_default_uflow),
   JUMP_INIT (pbackfail, _IO_str_pbackfail),
@@ -87,6 +88,7 @@ __open_memstream (char **bufloc, _IO_size_t *sizeloc)
       return NULL;
     }
   _IO_init_internal (&new_f->fp._sf._sbf._f, 0);
+  _IO_link_in ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf);
   _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps;
   _IO_str_init_static_internal (&new_f->fp._sf, buf, _IO_BUFSIZ, buf);
   new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
@@ -96,9 +98,6 @@ __open_memstream (char **bufloc, _IO_size_t *sizeloc)
   new_f->fp.bufloc = bufloc;
   new_f->fp.sizeloc = sizeloc;
 
-  /* Disable single thread optimization.  BZ 21735.  */
-  new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
-
   return (_IO_FILE *) &new_f->fp._sf._sbf;
 }
 libc_hidden_def (__open_memstream)
@@ -140,3 +139,17 @@ _IO_mem_finish (_IO_FILE *fp, int dummy)
 
   _IO_str_finish (fp, 0);
 }
+
+static int
+_IO_mem_overflow (_IO_FILE *fp, int c)
+{
+  if (c == EOF)
+    {
+      /* Updates the returned size location on stream flush.  */
+      struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
+      *mp->bufloc = fp->_IO_write_base;
+      *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
+      return 0;
+    }
+  return _IO_str_overflow (fp, c);
+}
diff --git a/libio/tst-memstream.h b/libio/tst-memstream.h
new file mode 100644
index 0000000..ffc2a65
--- /dev/null
+++ b/libio/tst-memstream.h
@@ -0,0 +1,66 @@
+/* Common definitions for open_memstream tests.
+   Copyright (C) 2017 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
+   <http://www.gnu.org/licenses/>.  */
+
+#include <mcheck.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <errno.h>
+
+#include <support/check.h>
+
+#ifdef TEST_WCHAR
+# include <wchar.h>
+
+/* Straighforward implementation so tst-memstream3 could use check
+   fwrite on open_memstream.  */
+static size_t __attribute__ ((used))
+fwwrite (const void *ptr, size_t size, size_t nmemb, FILE *arq)
+{
+  const wchar_t *wcs = (const wchar_t*) (ptr);
+  for (size_t s = 0; s < size; s++)
+    {
+      for (size_t n = 0; n < nmemb; n++)
+        if (fputwc (wcs[n], arq) == WEOF)
+          return n;
+    }
+  return size * nmemb;
+}
+
+# define CHAR_T wchar_t
+# define W(o) L##o
+# define OPEN_MEMSTREAM open_wmemstream
+# define PRINTF wprintf
+# define FWRITE fwwrite
+# define FPUTC  fputwc
+# define STRCMP wcscmp
+# define STRLEN wcslen
+#else
+# define CHAR_T char
+# define W(o) o
+# define OPEN_MEMSTREAM open_memstream
+# define PRINTF printf
+# define FWRITE fwrite
+# define FPUTC fputc
+# define STRCMP strcmp
+# define STRLEN strlen
+#endif
+
+#define S(s) S1 (s)
+#define S1(s) #s
diff --git a/libio/tst-memstream4.c b/libio/tst-memstream4.c
new file mode 100644
index 0000000..e98ca51
--- /dev/null
+++ b/libio/tst-memstream4.c
@@ -0,0 +1,99 @@
+/* Test for open_memstream BZ #21735.
+   Copyright (C) 2017 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
+   <http://www.gnu.org/licenses/>.  */
+
+#include "tst-memstream.h"
+
+static void
+mcheck_abort (enum mcheck_status ev)
+{
+  printf ("mecheck failed with status %d\n", (int) ev);
+  exit (1);
+}
+
+static int
+do_test (void)
+{
+  mcheck_pedantic (mcheck_abort);
+
+  CHAR_T *buf;
+  size_t size;
+
+  FILE *fp = OPEN_MEMSTREAM (&buf, &size);
+  if (fp == NULL)
+    FAIL_RET ("%s failed", S(OPEN_MEMSTREAM));
+
+  /* Basic test to check if 'fflush (NULL)' flushed all stream including
+     open_memstream ones.  */
+  {
+    if (FPUTC (W('a'), fp) != W('a'))
+      FAIL_RET ("%s failed: %m", S(FPUTC));
+
+    if (fflush (NULL) != 0)
+      FAIL_RET ("fflush failed: %m");
+
+    if (size != 1)
+      FAIL_RET ("size != 1");
+
+    TEST_VERIFY (STRCMP (buf, W("a")) == 0);
+  }
+
+  /* Same as before now with a buf close and equal to BUFSIZ.  */
+  {
+    const size_t bufsize = BUFSIZ - 1;
+    CHAR_T bufcmp[bufsize + 2];
+    size_t i;
+
+    bufcmp[0] = W('a');
+    for (i=1; i<bufsize; i++)
+      {
+	bufcmp[i] = W('a');
+	FPUTC (W('a'), fp);
+      }
+    bufcmp[i] = W('\0');
+
+    if (fflush (NULL) != 0)
+      FAIL_RET ("fflush failed: %m");
+
+    if (size != bufsize)
+      FAIL_RET ("size != %zu", bufsize);
+
+    TEST_VERIFY (STRCMP (buf, bufcmp) == 0);
+
+    bufcmp[i] = W('a');
+    bufcmp[i + 1] = W('\0');
+    FPUTC (W('a'), fp);
+
+    if (fflush (NULL) != 0)
+      FAIL_RET ("fflush failed: %m");
+
+    if (size != (bufsize + 1))
+      FAIL_RET ("size != %zu", bufsize);
+
+    TEST_VERIFY (STRCMP (buf, bufcmp) == 0);
+  }
+
+  if (fileno (fp) != -1)
+    FAIL_RET ("fileno returned a valid integer descriptor");
+
+  if (fclose (fp) != 0)
+    FAIL_RET ("fclose: %m");
+
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/libio/tst-wmemstream4.c b/libio/tst-wmemstream4.c
new file mode 100644
index 0000000..9dbf615
--- /dev/null
+++ b/libio/tst-wmemstream4.c
@@ -0,0 +1,20 @@
+/* Test for open_wmemstream BZ #21735.
+   Copyright (C) 2017 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
+   <http://www.gnu.org/licenses/>.  */
+
+#define TEST_WCHAR
+#include <libio/tst-memstream4.c>
diff --git a/libio/wmemstream.c b/libio/wmemstream.c
index 103a760..d68334b 100644
--- a/libio/wmemstream.c
+++ b/libio/wmemstream.c
@@ -32,13 +32,14 @@ struct _IO_FILE_wmemstream
 
 static int _IO_wmem_sync (_IO_FILE* fp) __THROW;
 static void _IO_wmem_finish (_IO_FILE* fp, int) __THROW;
+static int _IO_wmem_overflow (_IO_FILE *fp, int c) __THROW;
 
 
 static const struct _IO_jump_t _IO_wmem_jumps libio_vtable =
 {
   JUMP_INIT_DUMMY,
   JUMP_INIT (finish, _IO_wmem_finish),
-  JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow),
+  JUMP_INIT (overflow, (_IO_overflow_t) _IO_wmem_overflow),
   JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow),
   JUMP_INIT (uflow, (_IO_underflow_t) _IO_wdefault_uflow),
   JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
@@ -89,6 +90,7 @@ open_wmemstream (wchar_t **bufloc, _IO_size_t *sizeloc)
     }
   _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps);
   _IO_fwide (&new_f->fp._sf._sbf._f, 1);
+  _IO_link_in ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf);
   _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf,
 			_IO_BUFSIZ / sizeof (wchar_t), buf);
   new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF;
@@ -98,9 +100,6 @@ open_wmemstream (wchar_t **bufloc, _IO_size_t *sizeloc)
   new_f->fp.bufloc = bufloc;
   new_f->fp.sizeloc = sizeloc;
 
-  /* Disable single thread optimization.  BZ 21735.  */
-  new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
-
   return (_IO_FILE *) &new_f->fp._sf._sbf;
 }
 
@@ -145,3 +144,18 @@ _IO_wmem_finish (_IO_FILE *fp, int dummy)
 
   _IO_wstr_finish (fp, 0);
 }
+
+static int
+_IO_wmem_overflow (_IO_FILE *fp, int c)
+{
+  if (c == EOF)
+    {
+      /* Updates the returned size location on stream flush.  */
+      struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp;
+      *mp->bufloc = fp->_wide_data->_IO_write_base;
+      *mp->sizeloc = (fp->_wide_data->_IO_write_ptr
+		     - fp->_wide_data->_IO_write_base);
+      return 0;
+    }
+  return _IO_wstr_overflow (fp, c);
+}
-- 
2.7.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]