From 4ff27aafec200e4e31826c8b962d50681ce9f25e Mon Sep 17 00:00:00 2001
From: Niels Provos <provos@cvs.openbsd.org>
Date: Sun, 24 Sep 2000 19:13:27 +0000
Subject: prevent memory leak in fdalloc; fix by greg@nest.cx

---
 sys/kern/kern_descrip.c | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index f3f15ed04de..31109d62dd4 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: kern_descrip.c,v 1.20 2000/04/01 23:29:25 provos Exp $	*/
+/*	$OpenBSD: kern_descrip.c,v 1.21 2000/09/24 19:13:26 provos Exp $	*/
 /*	$NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $	*/
 
 /*
@@ -650,10 +650,6 @@ fdalloc(p, want, result)
 		    M_FILEDESC, M_WAITOK);
 		newofileflags = (char *) &newofile[nfiles];
 
-		MALLOC(newhimap, u_int *, NDHISLOTS(nfiles) * sizeof(u_int),
-		       M_FILEDESC, M_WAITOK);
-		MALLOC(newlomap, u_int *, NDLOSLOTS(nfiles) * sizeof(u_int),
-		       M_FILEDESC, M_WAITOK);
 		/*
 		 * Copy the existing ofile and ofileflags arrays
 		 * and zero the new portion of each array.
@@ -665,27 +661,37 @@ fdalloc(p, want, result)
 			(i = sizeof(char) * fdp->fd_nfiles));
 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
 
-		bcopy(fdp->fd_himap, newhimap,
-		      (i = NDHISLOTS(fdp->fd_nfiles) * sizeof(u_int)));
-		bzero((char *)newhimap + i,
-		      NDHISLOTS(nfiles) * sizeof(u_int) - i);
-
-		bcopy(fdp->fd_lomap, newlomap,
-		      (i = NDLOSLOTS(fdp->fd_nfiles) * sizeof(u_int)));
-		bzero((char *)newlomap + i,
-		      NDLOSLOTS(nfiles) * sizeof(u_int) - i);
-
 		if (fdp->fd_nfiles > NDFILE)
 			FREE(fdp->fd_ofiles, M_FILEDESC);
-		if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
-			FREE(fdp->fd_himap, M_FILEDESC);
-			FREE(fdp->fd_lomap, M_FILEDESC);
+
+		if (NDHISLOTS(nfiles) > NDHISLOTS(fdp->fd_nfiles)) {
+			MALLOC(newhimap, u_int *,
+			       NDHISLOTS(nfiles) * sizeof(u_int),
+			       M_FILEDESC, M_WAITOK);
+			MALLOC(newlomap, u_int *,
+			       NDLOSLOTS(nfiles) * sizeof(u_int),
+			       M_FILEDESC, M_WAITOK);
+
+			bcopy(fdp->fd_himap, newhimap,
+			      (i = NDHISLOTS(fdp->fd_nfiles) * sizeof(u_int)));
+			bzero((char *)newhimap + i,
+			      NDHISLOTS(nfiles) * sizeof(u_int) - i);
+
+			bcopy(fdp->fd_lomap, newlomap,
+			      (i = NDLOSLOTS(fdp->fd_nfiles) * sizeof(u_int)));
+			bzero((char *)newlomap + i,
+			      NDLOSLOTS(nfiles) * sizeof(u_int) - i);
+
+			if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
+				FREE(fdp->fd_himap, M_FILEDESC);
+				FREE(fdp->fd_lomap, M_FILEDESC);
+			}
+			fdp->fd_himap = newhimap;
+			fdp->fd_lomap = newlomap;
 		}
 		fdp->fd_ofiles = newofile;
 		fdp->fd_ofileflags = newofileflags;
 		fdp->fd_nfiles = nfiles;
-		fdp->fd_himap = newhimap;
-		fdp->fd_lomap = newlomap;
 		fdexpand++;
 	}
 }
-- 
cgit v1.2.3