From 691eb0d8edc17fbb4850a704244fed302282f931 Mon Sep 17 00:00:00 2001 From: Frank Richter Date: Sun, 19 Dec 2021 21:57:06 +0100 Subject: [PATCH] Add tests to check "freed memory fill" behaviour in debug mode --- test/test-api-fill.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/test-api-fill.c b/test/test-api-fill.c index 39f6f881..0e5a65dc 100644 --- a/test/test-api-fill.c +++ b/test/test-api-fill.c @@ -15,6 +15,7 @@ terms of the MIT license. A copy of the license can be found in the file bool check_zero_init(uint8_t* p, size_t size); #if MI_DEBUG >= 2 bool check_debug_fill_uninit(uint8_t* p, size_t size); +bool check_debug_fill_freed(uint8_t* p, size_t size); #endif // --------------------------------------------------------------------------- @@ -269,6 +270,22 @@ int main(void) { result &= check_debug_fill_uninit(p, malloc_size); mi_free(p); }); + + + CHECK_BODY("fill-freed-small", { + size_t malloc_size = MI_SMALL_SIZE_MAX / 2; + uint8_t* p = (uint8_t*)mi_malloc(malloc_size); + mi_free(p); + // First sizeof(void*) bytes will contain housekeeping data, skip these + result = check_debug_fill_freed(p + sizeof(void*), malloc_size - sizeof(void*)); + }); + CHECK_BODY("fill-freed-large", { + size_t malloc_size = MI_SMALL_SIZE_MAX * 2; + uint8_t* p = (uint8_t*)mi_malloc(malloc_size); + mi_free(p); + // First sizeof(void*) bytes will contain housekeeping data, skip these + result = check_debug_fill_freed(p + sizeof(void*), malloc_size - sizeof(void*)); + }); #endif // --------------------------------------------------- @@ -301,4 +318,15 @@ bool check_debug_fill_uninit(uint8_t* p, size_t size) { } return result; } + +bool check_debug_fill_freed(uint8_t* p, size_t size) { + if(!p) + return false; + + bool result = true; + for (size_t i = 0; i < size; ++i) { + result &= p[i] == MI_DEBUG_FREED; + } + return result; +} #endif