summary refs log tree commit diff
path: root/libio
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2017-07-13 16:43:24 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2018-10-02 10:01:56 -0300
commit0b727ed4d605d9318cb0d323c88abb0d5a441a9b (patch)
tree456efbab739f291b486ee8d937703cb0d4db5cef /libio
parentd0e91e8c4ef77997c940538eaa752799c9dda2d3 (diff)
downloadglibc-0b727ed4d605d9318cb0d323c88abb0d5a441a9b.tar.gz
glibc-0b727ed4d605d9318cb0d323c88abb0d5a441a9b.tar.xz
glibc-0b727ed4d605d9318cb0d323c88abb0d5a441a9b.zip
libio: Flush stream at freopen (BZ#21037)
As POSIX states [1] a freopen call should first flush the stream as if by a
call fflush.  C99 (n1256) and C11 (n1570) only states the function should
first close any file associated with the specific stream.  Although current
implementation only follow C specification, current BSD and other libc
implementation (musl) are in sync with POSIX and fflush the stream.

This patch change freopen{64} to fflush the stream before actually reopening
it (or returning if the stream does not support reopen).  It also changes the
Linux implementation to avoid a dynamic allocation on 'fd_to_filename'.

Checked on x86_64-linux-gnu.

	[BZ #21037]
	* libio/Makefile (tests): Add tst-memstream4 and tst-wmemstream4.
	* libio/freopen.c (freopen): Sync stream before reopen and adjust to
	new fd_to_filename interface.
	* libio/freopen64.c (freopen64): Likewise.
	* libio/tst-memstream.h: New file.
	* libio/tst-memstream4.c: Likewise.
	* libio/tst-wmemstream4.c: Likewise.
	* sysdeps/generic/fd_to_filename.h (fd_to_filename): Change signature.
	* sysdeps/unix/sysv/linux/fd_to_filename.h (fd_to_filename): Likewise
	and remove internal dynamic allocation.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/
Diffstat (limited to 'libio')
-rw-r--r--libio/Makefile4
-rw-r--r--libio/freopen.c29
-rw-r--r--libio/freopen64.c26
-rw-r--r--libio/tst-memstream.h68
-rw-r--r--libio/tst-memstream4.c64
-rw-r--r--libio/tst-wmemstream4.c20
6 files changed, 185 insertions, 26 deletions
diff --git a/libio/Makefile b/libio/Makefile
index cab0eae946..8239fba828 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -59,8 +59,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/freopen.c b/libio/freopen.c
index abf19e96f1..6ce74ae449 100644
--- a/libio/freopen.c
+++ b/libio/freopen.c
@@ -24,28 +24,34 @@
    This exception applies to code released by its copyright holders
    in files containing the exception.  */
 
-#include "libioP.h"
-#include "stdio.h"
+#include <stdio.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
 
-#include <shlib-compat.h>
+#include <libioP.h>
 #include <fd_to_filename.h>
-
-#include <kernel-features.h>
+#include <shlib-compat.h>
 
 FILE *
 freopen (const char *filename, const char *mode, FILE *fp)
 {
-  FILE *result;
+  FILE *result = NULL;
+  char fdfilename[FD_TO_FILENAME_SIZE];
+
   CHECK_FILE (fp, NULL);
-  if (!(fp->_flags & _IO_IS_FILEBUF))
-    return NULL;
+
   _IO_acquire_lock (fp);
+  /* First flush the stream (failure should be ignored).  */
+  _IO_SYNC (fp);
+
+  if (!(fp->_flags & _IO_IS_FILEBUF))
+    goto end;
+
   int fd = _IO_fileno (fp);
-  const char *gfilename = (filename == NULL && fd >= 0
-			   ? fd_to_filename (fd) : filename);
+  const char *gfilename
+    = filename != NULL ? filename : fd_to_filename (fd, fdfilename);
+
   fp->_flags2 |= _IO_FLAGS2_NOCLOSE;
 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
   if (&_IO_stdin_used == NULL)
@@ -101,9 +107,6 @@ freopen (const char *filename, const char *mode, FILE *fp)
     __close (fd);
 
 end:
-  if (filename == NULL)
-    free ((char *) gfilename);
-
   _IO_release_lock (fp);
   return result;
 }
diff --git a/libio/freopen64.c b/libio/freopen64.c
index ea7ebd3da4..055b999e59 100644
--- a/libio/freopen64.c
+++ b/libio/freopen64.c
@@ -24,27 +24,33 @@
    This exception applies to code released by its copyright holders
    in files containing the exception.  */
 
-#include "libioP.h"
-#include "stdio.h"
+#include <stdio.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
 
+#include <libioP.h>
 #include <fd_to_filename.h>
 
-#include <kernel-features.h>
-
 FILE *
 freopen64 (const char *filename, const char *mode, FILE *fp)
 {
-  FILE *result;
+  FILE *result = NULL;
+  char fdfilename[FD_TO_FILENAME_SIZE];
+
   CHECK_FILE (fp, NULL);
-  if (!(fp->_flags & _IO_IS_FILEBUF))
-    return NULL;
+
   _IO_acquire_lock (fp);
+  /* First flush the stream (failure should be ignored).  */
+  _IO_SYNC (fp);
+
+  if (!(fp->_flags & _IO_IS_FILEBUF))
+    goto end;
+
   int fd = _IO_fileno (fp);
-  const char *gfilename = (filename == NULL && fd >= 0
-			   ? fd_to_filename (fd) : filename);
+  const char *gfilename
+    = filename != NULL ? filename : fd_to_filename (fd, fdfilename);
+
   fp->_flags2 |= _IO_FLAGS2_NOCLOSE;
   _IO_file_close_it (fp);
   _IO_JUMPS_FILE_plus (fp) = &_IO_file_jumps;
@@ -84,8 +90,6 @@ freopen64 (const char *filename, const char *mode, FILE *fp)
     __close (fd);
 
 end:
-  if (filename == NULL)
-    free ((char *) gfilename);
   _IO_release_lock (fp);
   return result;
 }
diff --git a/libio/tst-memstream.h b/libio/tst-memstream.h
new file mode 100644
index 0000000000..b9b02bdb17
--- /dev/null
+++ b/libio/tst-memstream.h
@@ -0,0 +1,68 @@
+/* Common definitions for open_memstream tests.
+   Copyright (C) 2018 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 FPUTS  fputws
+# 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 FPUTS  fputs
+# 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 0000000000..a83a8bfed4
--- /dev/null
+++ b/libio/tst-memstream4.c
@@ -0,0 +1,64 @@
+/* Test for open_memstream BZ #21037.
+   Copyright (C) 2018 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);
+
+  /* Check if freopen proper fflush the stream.  */
+  {
+    CHAR_T old[] = W("old");
+    CHAR_T *buf = old;
+    size_t size;
+
+    FILE *fp = OPEN_MEMSTREAM (&buf, &size);
+    TEST_VERIFY_EXIT (fp != NULL);
+
+    FPUTS (W("new"), fp);
+    /* The stream buffer pointer should be updated with only a fflush or
+       fclose.  */
+    TEST_COMPARE (STRCMP (buf, old), 0);
+
+    /* The old stream should be fflush the stream, even for an invalid
+       streams.  */
+    FILE *nfp = freopen ("invalid-file", "r", fp);
+    TEST_VERIFY_EXIT (nfp == NULL);
+
+    TEST_VERIFY (STRCMP (buf, W("new")) == 0);
+    TEST_COMPARE_BLOB (buf, STRLEN (buf) * sizeof (CHAR_T),
+		       W("new"), STRLEN (W("new")) * sizeof (CHAR_T));
+
+    TEST_COMPARE (fclose (fp), 0);
+
+    free (buf);
+  }
+
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/libio/tst-wmemstream4.c b/libio/tst-wmemstream4.c
new file mode 100644
index 0000000000..8ff146e2d9
--- /dev/null
+++ b/libio/tst-wmemstream4.c
@@ -0,0 +1,20 @@
+/* Test for open_wmemstream BZ #21037.
+   Copyright (C) 2018 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>