Opened 5 years ago

Last modified 5 years ago

#344 new defect

fcntl(F_DUPFD) sets wrong errno

Reported by: dmik Owned by:
Priority: normal Milestone: libc-0.7
Component: libc Version: 0.6.6
Severity: normal Keywords:
Cc:

Description

If you pass an invalid file descriptor to the fcntl(F_DUPFD) call, it will return -1 and set errno to EPERM. This is wrong as according to the specs it should set it to EBADF in such a case (and it does so on *nix).

Here is the test case proving that:

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

int main()
{
    int fd = 100; // must be an invalid fd
    int flags = fcntl(fd, F_GETFL);
    printf ("fcntl(%d,F_GETFL)=%d errno=%d\n", fd, flags, errno);

    int fd2 = fcntl(fd, F_DUPFD, 200);
    printf ("dupfd(%d,200,F_DUPFD)=%d errno=%d\n", fd, fd2, errno);

    return 0;
}

Change History (1)

comment:1 Changed 5 years ago by dmik

There is at least one known failing case that I had to work around like this: http://trac.netlabs.org/ports/changeset/1125.

Note: See TracTickets for help on using tickets.