On 10/24/2012 10:12 AM, Uros Bizjak wrote:

Is it OK to call dup on the same FD the second time?

To answer my own question:

dup(4)                                  = 9
...
close(9)                                = 0
dup(4)                                  = -1 EBADF (Bad file descriptor)

Test is calling dup on a closed file descriptor. FD 4 and 9 share file
status flags.

I suspect that this is something else because this works:

open("/dev/null", O_RDONLY)             = 3
dup(3)                                  = 4
close(4)                                = 0
dup(3)                                  = 4

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

int
main(void)
{
  int fd1 = open("/dev/null", O_RDONLY);
  if (fd1 < 0) {
    perror("open");
    return 1;
  }
  int fd2 = dup(fd1);
  if (fd2 < 0) {
    perror("dup");
    return 1;
  }
  if (close(fd2) < 0) {
    perror("close");
    return 1;
  }
  int fd3 = dup(fd1);
  if (fd3 < 0) {
    perror("dup");
    return 1;
  }
  return 0;
}



--
Florian Weimer / Red Hat Product Security Team

Reply via email to