summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorotto <>2008-08-22 21:25:10 +0000
committerotto <>2008-08-22 21:25:10 +0000
commitf77577b586a3b656d0d3fd7cd01ec0fa114cd116 (patch)
treeaafe58421a299687392de0a5e42d7981053ad583
parente6c446cc76e761b92735aca89f612db2ed9a52d2 (diff)
downloadopenbsd-f77577b586a3b656d0d3fd7cd01ec0fa114cd116.tar.gz
openbsd-f77577b586a3b656d0d3fd7cd01ec0fa114cd116.tar.bz2
openbsd-f77577b586a3b656d0d3fd7cd01ec0fa114cd116.zip
make sure we always map and unmap multiples of MALLOC_PAGESIZE;
case spotted by beck, one by me; ok deraadt@ beck@
-rw-r--r--src/lib/libc/stdlib/malloc.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index d03b831514..4379e09157 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc.c,v 1.94 2008/08/22 17:14:57 otto Exp $ */ 1/* $OpenBSD: malloc.c,v 1.95 2008/08/22 21:25:10 otto Exp $ */
2/* 2/*
3 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
4 * 4 *
@@ -382,11 +382,16 @@ wrtwarning(char *p)
382static void 382static void
383unmap(struct dir_info *d, void *p, size_t sz) 383unmap(struct dir_info *d, void *p, size_t sz)
384{ 384{
385 size_t psz = PAGEROUND(sz) >> MALLOC_PAGESHIFT; 385 size_t psz = sz >> MALLOC_PAGESHIFT;
386 size_t rsz, tounmap; 386 size_t rsz, tounmap;
387 struct region_info *r; 387 struct region_info *r;
388 u_int i, offset; 388 u_int i, offset;
389 389
390 if (sz != PAGEROUND(sz)) {
391 wrterror("munmap round");
392 return;
393 }
394
390 if (psz > malloc_cache) { 395 if (psz > malloc_cache) {
391 if (munmap(p, sz)) 396 if (munmap(p, sz))
392 wrterror("munmap"); 397 wrterror("munmap");
@@ -445,11 +450,15 @@ unmap(struct dir_info *d, void *p, size_t sz)
445static void * 450static void *
446map(struct dir_info *d, size_t sz, int zero_fill) 451map(struct dir_info *d, size_t sz, int zero_fill)
447{ 452{
448 size_t psz = PAGEROUND(sz) >> MALLOC_PAGESHIFT; 453 size_t psz = sz >> MALLOC_PAGESHIFT;
449 struct region_info *r, *big = NULL; 454 struct region_info *r, *big = NULL;
450 u_int i, offset; 455 u_int i, offset;
451 void *p; 456 void *p;
452 457
458 if (sz != PAGEROUND(sz)) {
459 wrterror("map round");
460 return NULL;
461 }
453 if (psz > d->free_regions_size) { 462 if (psz > d->free_regions_size) {
454 p = MMAP(sz); 463 p = MMAP(sz);
455 if (p != MAP_FAILED) 464 if (p != MAP_FAILED)
@@ -1065,7 +1074,7 @@ omalloc(size_t sz, int zero_fill)
1065 return NULL; 1074 return NULL;
1066 } 1075 }
1067 if (insert(&g_pool, p, sz)) { 1076 if (insert(&g_pool, p, sz)) {
1068 unmap(&g_pool, p, sz); 1077 unmap(&g_pool, p, psz);
1069 errno = ENOMEM; 1078 errno = ENOMEM;
1070 return NULL; 1079 return NULL;
1071 } 1080 }
@@ -1181,7 +1190,7 @@ ofree(void *p)
1181 } 1190 }
1182 if (malloc_junk) 1191 if (malloc_junk)
1183 memset(p, SOME_FREEJUNK, PAGEROUND(sz) - malloc_guard); 1192 memset(p, SOME_FREEJUNK, PAGEROUND(sz) - malloc_guard);
1184 unmap(&g_pool, p, sz); 1193 unmap(&g_pool, p, PAGEROUND(sz));
1185 delete(&g_pool, r); 1194 delete(&g_pool, r);
1186 } else { 1195 } else {
1187 void *tmp; 1196 void *tmp;