From d47f3fdb0ae7803a590f0081e5667ff094eaa040 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Thu, 29 Nov 2018 08:53:39 +0000 Subject: [PATCH] fd: unify fd range check across the routines While here annotate out of range as unlikely. Sponsored by: The FreeBSD Foundation --- sys/kern/kern_descrip.c | 2 +- sys/sys/filedesc.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index e0721a223eae..61dd908144f3 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -2637,7 +2637,7 @@ fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp, #endif fdt = fdp->fd_files; - if ((u_int)fd >= fdt->fdt_nfiles) + if (__predict_false((u_int)fd >= fdt->fdt_nfiles)) return (EBADF); /* * Fetch the descriptor locklessly. We avoid fdrop() races by diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h index db5debcfe7c8..541f5f6c98df 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -208,7 +208,7 @@ fget_locked(struct filedesc *fdp, int fd) FILEDESC_LOCK_ASSERT(fdp); - if (fd < 0 || fd > fdp->fd_lastfile) + if (__predict_false((u_int)fd >= fdp->fd_nfiles)) return (NULL); return (fdp->fd_ofiles[fd].fde_file); @@ -221,11 +221,11 @@ fdeget_locked(struct filedesc *fdp, int fd) FILEDESC_LOCK_ASSERT(fdp); - if (fd < 0 || fd > fdp->fd_lastfile) + if (__predict_false((u_int)fd >= fdp->fd_nfiles)) return (NULL); fde = &fdp->fd_ofiles[fd]; - if (fde->fde_file == NULL) + if (__predict_false(fde->fde_file == NULL)) return (NULL); return (fde);