This is the mail archive of the 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

--- Comment #1 from lizhijian <lizhijian at cn dot> ---
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
                        return -1;
        if (fstat(fileno(fp2), &stat_buf) == -1) {
                printf("[E+] freopen return success, but the newfd was
                return -1;

        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

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]