From 1137ba9b32ee93973065a3dde8c0168d14df24c3 Mon Sep 17 00:00:00 2001 From: Erik van der Kouwe Date: Mon, 3 May 2010 19:42:08 +0000 Subject: [PATCH] Extra assertions on free if SLOWDEBUG is enabled: check whether the block exists and has not been freed before --- lib/libc/ansi/malloc.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/libc/ansi/malloc.c b/lib/libc/ansi/malloc.c index 694fcdc61..2c53dcbaa 100644 --- a/lib/libc/ansi/malloc.c +++ b/lib/libc/ansi/malloc.c @@ -2,7 +2,7 @@ /* replace undef by define */ #define DEBUG /* check assertions */ -#undef SLOWDEBUG /* some extra test loops (requires DEBUG) */ +#define SLOWDEBUG /* some extra test loops (requires DEBUG) */ #ifndef DEBUG #define NDEBUG @@ -179,6 +179,34 @@ free(void *ptr) if (p == 0) return; +#ifdef SLOWDEBUG + { + int found; + char *curr; + + /* block must be in block list */ + assert(_bottom); + found = 0; + for (curr = _bottom; (next = NextSlot(curr)) != 0; curr = next) { + assert(next > curr); + if (curr == p) found = 1; + } + if (curr == p) found = 1; + assert(found); + + /* block must not be in free list */ + if (_empty) { + found = 0; + for (curr = _empty; (next = NextFree(curr)) != 0; curr = next) { + assert(next > curr); + if (curr == p) found = 1; + } + if (curr == p) found = 1; + assert(!found); + } + } +#endif + assert((char *) NextSlot(p) > p); for (prev = 0, next = _empty; next != 0; prev = next, next = NextFree(next)) if (p < next) -- 2.44.0