Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 29 Nov 2018 08:53:39 +0000 (UTC)
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r341219 - in head/sys: kern sys
Message-ID:  <201811290853.wAT8rdds092312@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mjg
Date: Thu Nov 29 08:53:39 2018
New Revision: 341219
URL: https://svnweb.freebsd.org/changeset/base/341219

Log:
  fd: unify fd range check across the routines
  
  While here annotate out of range as unlikely.
  
  Sponsored by:	The FreeBSD Foundation

Modified:
  head/sys/kern/kern_descrip.c
  head/sys/sys/filedesc.h

Modified: head/sys/kern/kern_descrip.c
==============================================================================
--- head/sys/kern/kern_descrip.c	Thu Nov 29 08:37:33 2018	(r341218)
+++ head/sys/kern/kern_descrip.c	Thu Nov 29 08:53:39 2018	(r341219)
@@ -2637,7 +2637,7 @@ fget_unlocked(struct filedesc *fdp, int fd, cap_rights
 #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

Modified: head/sys/sys/filedesc.h
==============================================================================
--- head/sys/sys/filedesc.h	Thu Nov 29 08:37:33 2018	(r341218)
+++ head/sys/sys/filedesc.h	Thu Nov 29 08:53:39 2018	(r341219)
@@ -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);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201811290853.wAT8rdds092312>