[Concept,11/19] test: common: Fix memory leak in malloc_fill_pool test

Message ID 20260314231618.338113-12-sjg@u-boot.org
State New
Headers
Series test: Fix pytest inter-test side effects |

Commit Message

Simon Glass March 14, 2026, 11:16 p.m. UTC
  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
  

Patch

diff --git a/test/common/malloc.c b/test/common/malloc.c
index af1a7326272..c4764a51f6b 100644
--- a/test/common/malloc.c
+++ b/test/common/malloc.c
@@ -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());