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]

Re: [PATCH][BZ #15589] - freopen would close oldfd even though oldfd is same as newfd.


On Sun, Sep 22, 2013 at 04:29:17PM +0000, Joseph S. Myers wrote:
> This looks like a bug for which a testcase could easily be added to the 
> glibc testsuite, so the patch should add such a test that fails before and 
> passes after the patch.
> 
> -- 
> Joseph S. Myers
> joseph@codesourcery.com

There was testcase in bugzilla, after running it through formatter and
creating files by fopen('w') instead touch you could use following:

	* libio/test-freopen.c: Add testcase when old fd is same as new fd.

---
 libio/test-freopen.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/libio/test-freopen.c b/libio/test-freopen.c
index 6ae194e..e9ef83a 100644
--- a/libio/test-freopen.c
+++ b/libio/test-freopen.c
@@ -20,6 +20,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <wchar.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
 
 
 int
@@ -52,5 +55,50 @@ main (int argc, char *argv[])
 
   fclose (fp);
 
-  return 0;
+  char *file1 = "/tmp/file1";
+  char *file2 = "/tmp/file2";
+  FILE *fp1, *fp2;
+  struct stat stat_buf;
+  int fail = 0;
+
+  fp1 = fopen (file1, "w");
+  fclose (fp1);
+  fp2 = fopen (file2, "w");
+  fclose (fp2);
+
+  if ((fp1 = freopen (file1, "r", stdin)) == NULL)
+    {
+      printf ("[E+] freopen error(%s)\n", strerror (errno));
+      fail = -1;
+    }
+  if (fstat (fileno (stdin), &stat_buf) == 0)
+    {
+      printf ("[S+] close stdin\n");
+      close (0);           /* close stdin */
+    }
+
+  if (fstat (fileno (stdin), &stat_buf) == -1)
+    {
+      printf ("[S+] stat return error(%s)\n", strerror (errno));
+      if ((fp2 = freopen (file2, "r", stdin)) == NULL)
+	{
+	  printf ("[E+] freopen return error(%s)\n", strerror (errno));
+	  fail = -1;
+	}
+    }
+  if (fstat (fileno (fp2), &stat_buf) == -1)
+    {
+      printf ("[E+] freopen return success, but the newfd was closed\n");
+      fail = -1;
+    }
+
+  fclose (fp1);
+  fclose (fp2);
+  if (!fail)
+    printf ("[S+] freopen test PASS\n");
+
+  unlink (file1);
+  unlink (file2);
+
+  return fail;
 }
-- 
1.8.3.2


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