[Concept,11/19] test: common: Fix memory leak in malloc_fill_pool test
Commit Message
From: Simon Glass <sjg@chromium.org>
When common_test_malloc_fill_pool() fills the heap and then fails an
assertion (e.g. because earlier tests leaked memory, reducing the
available pool), it returns early without freeing the allocations.
This leaks ~120 MB — the entire malloc pool — causing all subsequent
tests that need malloc to fail.
Free all allocations before checking the peak, so that a test failure
does not leak the entire pool and break all subsequent tests.
Signed-off-by: Simon Glass <sjg@chromium.org>
---
test/common/malloc.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
--
2.43.0
@@ -616,18 +616,24 @@ static int common_test_malloc_fill_pool(struct unit_test_state *uts)
ptr_table_size);
/*
- * Should have allocated most of the pool - if we can't allocate
- * 1MB, then at most 1MB is available, so we must have allocated
- * at least (pool_size - 1MB)
+ * Should have allocated most of the pool - if we can't allocate 1MB,
+ * then at most 1MB is available, so we must have allocated at least
+ * (pool_size - 1MB). Save the peak before freeing so an assertion
+ * failure does not leak the entire pool.
*/
ut_assert(count > 0);
ut_assert(count < ptr_table_size / sizeof(void *));
- ut_assert(get_alloced_size() >= TOTAL_MALLOC_LEN - SZ_1M);
+ alloc_size = get_alloced_size();
- /* Free all allocations */
+ /*
+ * Free all allocations before checking the peak, so that a failure does
+ * not leak the entire pool and break later tests
+ */
for (i = 0; i < count; i++)
free(ptrs[i]);
+ ut_assert(alloc_size >= TOTAL_MALLOC_LEN - SZ_1M);
+
/* Should be back to starting state */
ut_asserteq(before, get_alloced_size());