svn commit: r327781 - head/sys/ufs/ufs

Pedro F. Giffuni pfg at FreeBSD.org
Wed Jan 10 19:45:39 UTC 2018


Author: pfg
Date: Wed Jan 10 19:45:38 2018
New Revision: 327781
URL: https://svnweb.freebsd.org/changeset/base/327781

Log:
  Use mallocarray(9) in dirhash.
  
  Basic use of mallocarray to prevent overflows. Here allocation is done
  with M_NOWAIT so the code is prepared for the possibility of returning
  NULL values. Since mallocarray expects unsigned parameters, unsign some
  related variables to minimize sign conversions.
  
  Reviewed by:	mckusick

Modified:
  head/sys/ufs/ufs/ufs_dirhash.c

Modified: head/sys/ufs/ufs/ufs_dirhash.c
==============================================================================
--- head/sys/ufs/ufs/ufs_dirhash.c	Wed Jan 10 19:41:05 2018	(r327780)
+++ head/sys/ufs/ufs/ufs_dirhash.c	Wed Jan 10 19:45:38 2018	(r327781)
@@ -349,7 +349,8 @@ ufsdirhash_build(struct inode *ip)
 	struct direct *ep;
 	struct vnode *vp;
 	doff_t bmask, pos;
-	int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot;
+	int j, memreqd, slot;
+	u_int dirblocks, i, nblocks, narrays, nslots;
 
 	/* Take care of a decreased sysctl value. */
 	while (ufs_dirhashmem > ufs_dirhashmaxmem) {
@@ -415,11 +416,11 @@ ufsdirhash_build(struct inode *ip)
 	 * Use non-blocking mallocs so that we will revert to a linear
 	 * lookup on failure rather than potentially blocking forever.
 	 */
-	dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]),
+	dh->dh_hash = mallocarray(narrays, sizeof(dh->dh_hash[0]),
 	    M_DIRHASH, M_NOWAIT | M_ZERO);
 	if (dh->dh_hash == NULL)
 		goto fail;
-	dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]),
+	dh->dh_blkfree = mallocarray(nblocks, sizeof(dh->dh_blkfree[0]),
 	    M_DIRHASH, M_NOWAIT);
 	if (dh->dh_blkfree == NULL)
 		goto fail;


More information about the svn-src-head mailing list