This is the mail archive of the glibc-bugs@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]

[Bug libc/15589] freopen would close oldfd even though oldfd is same as newfd


http://sourceware.org/bugzilla/show_bug.cgi?id=15589

--- Comment #1 from lizhijian <lizhijian at cn dot fujitsu.com> ---
A simple C program(freopen-test.c) to reproduce this problem.

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>

int main(void)
{
        char *file1 = "./file1";
        char *file2 = "./file2";
        FILE *fp1, *fp2;
        struct stat stat_buf;

        if((fp1 = freopen(file1, "r", stdin)) == NULL)
                printf("[E+] freopen error(%s)\n", strerror(errno));

        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));
                        return -1;
                }
        }
        if (fstat(fileno(fp2), &stat_buf) == -1) {
                printf("[E+] freopen return success, but the newfd was
closed\n");
                return -1;
        }

        fclose(fp1);
        fclose(fp2);
        printf("[S+] freopen test PASS\n");
}
------------------------
# gcc freopen-test.c -o freopen-test
# touch file1 file2
# ./freopen-test
[E+] freopen error(No such file or directory)
[S+] stat return error(Bad file descriptor)
[E+] freopen return error(No such file or directory)

# rpm -q glibc
glibc-2.16-29.el7.x86_64

-- 
You are receiving this mail because:
You are on the CC list for the bug.


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