rtemsbsd/libio: Handle invalid descriptors

The documentation for this function suggests that it can handle invalid
descriptors safely. This change allows negative descriptors to be
handled without a crash.
This commit is contained in:
Kinsey Moore 2023-09-19 16:02:47 -05:00 committed by Joel Sherrill
parent 7cc487f2d3
commit f0fe0439c4

View File

@ -228,7 +228,7 @@ rtems_bsd_libio_iop_hold(int fd, rtems_libio_t **iopp)
rtems_libio_t *iop = NULL; rtems_libio_t *iop = NULL;
unsigned int flags = 0; unsigned int flags = 0;
int ffd = -1; int ffd = -1;
if (fd < rtems_libio_number_iops) { if (fd >= 0 && fd < rtems_libio_number_iops) {
iop = rtems_libio_iop(fd); iop = rtems_libio_iop(fd);
flags = rtems_libio_iop_hold(iop); flags = rtems_libio_iop_hold(iop);
if ((flags & LIBIO_FLAGS_OPEN) != 0) { if ((flags & LIBIO_FLAGS_OPEN) != 0) {
@ -249,7 +249,9 @@ rtems_bsd_libio_iop_hold(int fd, rtems_libio_t **iopp)
if (RTEMS_BSD_DESCRIP_TRACE) if (RTEMS_BSD_DESCRIP_TRACE)
flags = iop->flags; flags = iop->flags;
} else { } else {
*iopp = NULL; if (iopp != NULL) {
*iopp = NULL;
}
} }
if (RTEMS_BSD_DESCRIP_TRACE) if (RTEMS_BSD_DESCRIP_TRACE)
printf("bsd: iop: hold: fd=%d ffd=%d refs=%d iop=%p by %p\n", printf("bsd: iop: hold: fd=%d ffd=%d refs=%d iop=%p by %p\n",