Issue created by Sang Woo Kim: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5669



Found while testing descriptor duplication for an EPICS IOC, measured on 
arm/xilinx_zynq_a9_qemu.

`dup2()` in `cpukit/libcsupport/src/dup2.c` runs `fstat( fildes2, &buf )` 
before doing anything and returns the failure, so duplicating onto a descriptor 
that is not open — the canonical POSIX use of dup2(), which specifies that an 
unopen fildes2 is simply not closed — always fails:

```c
int s = socket(AF_INET, SOCK_DGRAM, 0);
int f = open("/tmp-probe", O_CREAT | O_RDWR, 0644);
close(f);
dup2(s, f);        /* -1, errno = EBADF */
```

POSIX only requires EBADF when fildes2 is outside the valid descriptor range, 
so the pre-check should be a range check, not fstat. Note that removing the 
pre-check alone is not enough: the underlying `fcntl(F_DUP2FD)` path cannot 
handle a free target descriptor either — that is the separate defect in the 
fcntl issue filed alongside this one.

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5669
You're receiving this email because of your account on gitlab.rtems.org. 
Unsubscribe from this thread: 
https://gitlab.rtems.org/-/sent_notifications/4-crhbcec24wyo5mof77ac3v3pt-1d/unsubscribe
 | Manage all notifications: https://gitlab.rtems.org/-/profile/notifications | 
Help: https://gitlab.rtems.org/help


_______________________________________________
bugs mailing list
[email protected]
http://lists.rtems.org/mailman/listinfo/bugs

Reply via email to