This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH][BZ #15589] - freopen would close oldfd even though oldfd is same as newfd.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: lizhijian at cn dot fujitsu dot com, libc-alpha at sourceware dot org
- Date: Sun, 22 Sep 2013 18:57:02 +0200
- Subject: Re: [PATCH][BZ #15589] - freopen would close oldfd even though oldfd is same as newfd.
- Authentication-results: sourceware.org; auth=none
- References: <20130921194736 dot GA9711 at domone dot kolej dot mff dot cuni dot cz> <Pine dot LNX dot 4 dot 64 dot 1309221628530 dot 29563 at digraph dot polyomino dot org dot uk>
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