RTEMS | dup2() fails with EBADF when fildes2 is not open (#5669)
Sang Woo Kim (@physwkim)
gitlab at rtems.org
Tue Jul 28 14:24:57 UTC 2026
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/bugs/attachments/20260728/17cea6be/attachment.htm>
More information about the bugs
mailing list