[issue21191] os.fdopen() may eat file descriptor and still raise exception

Dima Tisnek report at bugs.python.org
Thu Apr 10 19:46:51 CEST 2014


Dima Tisnek added the comment:

I'm not sure if you are referring to Python's C-level fdopen or GNU libc fdopen.

GNU libc fdopen does not consume file descriptor on error:

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


int main(int argc, char** argv)
{
    int fd = -1;
    int rv = 0;
    FILE* fh = NULL;
    if (argc<3) return 1;

    errno = 0;
    fd = open(argv[1], O_RDONLY);
    printf("got fd %d errno %d text %s\n", fd, errno, strerror(errno));

    errno = 0;
    fh = fdopen(fd, argv[2]);
    printf("got fh %x errno %d text %s\n", fh, errno, strerror(errno));

    errno = 0;
    rv = close(fd);
    printf("got rv %d errno %d text %s\n", rv, errno, strerror(errno));
}

[dima at bmg ~]$ ./a.out /etc/passwd w
got fd 4 errno 0 text Success
got fh 0 errno 22 text Invalid argument
got rv 0 errno 0 text Success

To be fair, GNU libc fdopen doesn't consider it an error to use a file descriptor that refers to a directory, which is the crux of this bug.

Anyhow, point is the semantics change your patch brings in sets Python 2.7+ in contrast with both Python 3.x and GNU libc. 

Perhaps if it's too hard to implement properly, it's better to leave this issue as won't fix / technical limitation?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21191>
_______________________________________


More information about the Python-bugs-list mailing list