summaryrefslogtreecommitdiff
path: root/sys/kern/kern_descrip.c
diff options
context:
space:
mode:
authorNiels Provos <provos@cvs.openbsd.org>2000-09-24 19:13:27 +0000
committerNiels Provos <provos@cvs.openbsd.org>2000-09-24 19:13:27 +0000
commit4ff27aafec200e4e31826c8b962d50681ce9f25e (patch)
treeec4c9f77e5a3ab15555c999b648d9e2e0418e965 /sys/kern/kern_descrip.c
parent71d64850e58311b24e4a7020129ae1bfa9db23ba (diff)
prevent memory leak in fdalloc; fix by greg@nest.cx
Diffstat (limited to 'sys/kern/kern_descrip.c')
-rw-r--r--sys/kern/kern_descrip.c46
1 files 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++;
}
}