This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


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] Fix freopen{,64}


Hi!

The testcase bellow distilled from mutt fails, because f initially
is using mmap stdio and freopen does not reset f's jump tables, so
it ended up with a rw stream which had nothing mmaped and was using mmap
stdio methods.

2002-07-09  Jakub Jelinek  <jakub@redhat.com>

	* libio/freopen.c (freopen): Reset jump tables, use mmap stdio for the
	new stream if possible.
	* libio/freopen64.c (freopen64): Likewise.
	* libio/Makefile (tests): Add tst-freopen.
	* libio/tst-freopen.c: New test.

--- libc/libio/freopen.c.jj	Thu Aug 23 18:47:55 2001
+++ libc/libio/freopen.c	Tue Jul  9 15:53:56 2002
@@ -1,4 +1,4 @@
-/* Copyright (C) 1993,95,96,97,98,2000,2001 Free Software Foundation, Inc.
+/* Copyright (C) 1993,95,96,97,98,2000,2001,2002 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
@@ -53,15 +53,26 @@ freopen (filename, mode, fp)
     }
 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
   if (&_IO_stdin_used == NULL)
-    /* If the shared C library is used by the application binary which
-       was linked against the older version of libio, we just use the
-       older one even for internal use to avoid trouble since a pointer
-       to the old libio may be passed into shared C library and wind
-       up here. */
-    result = _IO_old_freopen (filename, mode, fp);
+    {
+      /* If the shared C library is used by the application binary which
+	 was linked against the older version of libio, we just use the
+	 older one even for internal use to avoid trouble since a pointer
+	 to the old libio may be passed into shared C library and wind
+	 up here. */
+      _IO_old_file_close_it (fp);
+      _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &_IO_old_file_jumps;
+      result = _IO_old_file_fopen (fp, filename, mode);
+    }
   else
 #endif
-    result = _IO_freopen (filename, mode, fp);
+    {
+      INTUSE(_IO_file_close_it) (fp);
+      _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &INTUSE(_IO_file_jumps);
+      fp->_wide_data->_wide_vtable = &INTUSE(_IO_wfile_jumps);
+      result = INTUSE(_IO_file_fopen) (fp, filename, mode, 1);
+      if (result != NULL)
+	result = __fopen_maybe_mmap (result);
+    }
   if (result != NULL)
     /* unbound stream orientation */
     result->_mode = 0;
--- libc/libio/freopen64.c.jj	Thu Aug 23 18:47:55 2001
+++ libc/libio/freopen64.c	Tue Jul  9 15:55:11 2002
@@ -1,4 +1,5 @@
-/* Copyright (C) 1993,1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
+/* Copyright (C) 1993,1995,1996,1997,1998,2000,2001,2002
+   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
@@ -51,7 +52,12 @@ freopen64 (filename, mode, fp)
       if (fd != -1)
 	filename = fd_to_filename (fd);
     }
-  result = _IO_freopen64 (filename, mode, fp);
+  INTUSE(_IO_file_close_it) (fp);
+  _IO_JUMPS ((struct _IO_FILE_plus *) fp) = &INTUSE(_IO_file_jumps);
+  fp->_wide_data->_wide_vtable = &INTUSE(_IO_wfile_jumps);
+  result = INTUSE(_IO_file_fopen) (fp, filename, mode, 0);
+  if (result != NULL)
+    result = __fopen_maybe_mmap (result);
   if (result != NULL)
     /* unbound stream orientation */
     result->_mode = 0;
--- libc/libio/Makefile.jj	Mon Jul  1 12:35:36 2002
+++ libc/libio/Makefile	Tue Jul  9 15:08:27 2002
@@ -49,7 +49,8 @@ routines	:=							      \
 tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
 	tst_wprintf2 tst-widetext test-fmemopen tst-ext tst-fopenloc	      \
 	tst-fgetws tst-ungetwc1 tst-ungetwc2 tst-swscanf tst-sscanf	      \
-	tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof
+	tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof          \
+	tst-freopen
 test-srcs = test-freopen
 
 all: # Make this the default target; it will be defined in Rules.
--- libc/libio/tst-freopen.c.jj	Tue Jul  9 15:00:39 2002
+++ libc/libio/tst-freopen.c	Tue Jul  9 15:07:13 2002
@@ -0,0 +1,103 @@
+/* Test freopen with mmap stdio.
+   Copyright (C) 2002 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
+
+   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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int main (void)
+{
+  char name[] = "/tmp/tst-freopen.XXXXXX";
+  char buf[4096];
+  const char * const test = "Let's test freopen.\n";
+  char temp[strlen (test) + 1];
+  int fd = mkstemp (name);
+  FILE *f;
+
+  if (fd == -1)
+    {
+      printf ("%Zd: cannot open temporary file: %m\n", __LINE__);
+      exit (1);
+    }
+
+  f = fdopen (fd, "w");
+  if (f == NULL)
+    {
+      printf ("%Zd: cannot fdopen temporary file: %m\n", __LINE__);
+      exit (1);
+    }
+
+  fputs (test, f);
+  fclose (f);
+
+  f = fopen (name, "r");
+  if (f == NULL)
+    {
+      printf ("%Zd: cannot fopen temporary file: %m\n", __LINE__);
+      exit (1);
+    }
+
+  if (fread (temp, 1, strlen (test), f) != strlen (test))
+    {
+      printf ("%Zd: couldn't read the file back: %m\n", __LINE__);
+      exit (1);
+    }
+  temp [strlen (test)] = '\0';
+
+  if (strcmp (test, temp))
+    {
+      printf ("%Zd: read different string than was written:\n%s%s",
+	      __LINE__, test, temp);
+      exit (1);
+    }
+
+  f = freopen (name, "r+", f);
+  if (f == NULL)
+    {
+      printf ("%Zd: cannot freopen temporary file: %m\n", __LINE__);
+      exit (1);
+    }
+
+  if (fseek (f, 0, SEEK_SET) != 0)
+    {
+      printf ("%Zd: couldn't fseek to start: %m\n", __LINE__);
+      exit (1);
+    }
+
+  if (fread (temp, 1, strlen (test), f) != strlen (test))
+    {
+      printf ("%Zd: couldn't read the file back: %m\n", __LINE__);
+      exit (1);
+    }
+  temp [strlen (test)] = '\0';
+
+  if (strcmp (test, temp))
+    {
+      printf ("%Zd: read different string than was written:\n%s%s",
+	      __LINE__, test, temp);
+      exit (1);
+    }
+
+  fclose (f);
+
+  unlink (name);
+  exit (0);
+}

	Jakub


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