From 1c1c1677e0e8dfb4e1aff08e19b0cf00ab8456d0 Mon Sep 17 00:00:00 2001 From: otto Date: Mon, 25 Aug 2008 17:56:17 +0000 Subject: [PATCH] Make all combinations of G, P, J and zero-fill work with as little effort as possible in most cases; ok djm@ --- lib/libc/stdlib/malloc.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index 1cbe7fa0842..be80640e813 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: malloc.c,v 1.97 2008/08/23 07:49:38 djm Exp $ */ +/* $OpenBSD: malloc.c,v 1.98 2008/08/25 17:56:17 otto Exp $ */ /* * Copyright (c) 2008 Otto Moerbeek * @@ -1078,8 +1078,6 @@ omalloc(size_t sz, int zero_fill) errno = ENOMEM; return NULL; } - if (malloc_junk) - memset(p + sz, SOME_JUNK, psz - sz); if (malloc_guard) { if (mprotect((char *)p + psz - malloc_guard, malloc_guard, PROT_NONE)) @@ -1087,11 +1085,28 @@ omalloc(size_t sz, int zero_fill) malloc_guarded += malloc_guard; } - /* shift towards the end */ if (malloc_move && - sz - malloc_guard < MALLOC_PAGESIZE - MALLOC_MINSIZE) + sz - malloc_guard < MALLOC_PAGESIZE - MALLOC_MINSIZE) { + /* fill whole allocation */ + if (malloc_junk) + memset(p, SOME_JUNK, psz - malloc_guard); + /* shift towards the end */ p = ((char *)p) + ((MALLOC_PAGESIZE - MALLOC_MINSIZE - (sz - malloc_guard)) & ~(MALLOC_MINSIZE-1)); + /* fill zeros if needed and overwritten above */ + if (zero_fill && malloc_junk) + memset(p, 0, sz - malloc_guard); + } else { + if (malloc_junk) { + if (zero_fill) + memset(p + sz - malloc_guard, + SOME_JUNK, psz - sz); + else + memset(p, + SOME_JUNK, psz - malloc_guard); + } + } + } else { /* takes care of SOME_JUNK */ p = malloc_bytes(&g_pool, sz); -- 2.20.1