Skip to content

Commit 6fcd045

Browse files
committed
tests: kernel: mem_heap: enhance k_heap_free() API coverage
Enhance k_heap_free() API coverage by testing NULL free & double free edge cases Signed-off-by: Abderrahmane JARMOUNI <git@jarmouni.me>
1 parent 4ef69bf commit 6fcd045

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

tests/kernel/mem_heap/k_heap_api/src/test_kheap_api.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ ZTEST(k_heap_api, test_k_heap_alloc_fail)
156156
* @details The test validates k_heap_free()
157157
* API, by using below steps
158158
* 1. allocate the memory from the heap,
159-
* 2. free the allocated memory
160-
* 3. allocate memory more than the first allocation.
161-
* the allocation in the 3rd step should succeed if k_heap_free()
159+
* 2. Freeing a NULL pointer (should have no effect)
160+
* 3. free the allocated memory
161+
* 4. allocate memory more than the first allocation.
162+
* 5. Double-freeing a pointer (asserts, but should not crash)
163+
* the allocation in the 4th step should succeed if k_heap_free()
162164
* works as expected
163165
*
164166
* @see k_heap_alloc, k_heap_free()
@@ -169,13 +171,22 @@ ZTEST(k_heap_api, test_k_heap_free)
169171
char *p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_1, timeout);
170172

171173
zassert_not_null(p, "k_heap_alloc operation failed");
174+
175+
/* Free NULL pointer: should not crash or corrupt heap */
176+
k_heap_free(&k_heap_test, NULL);
177+
172178
k_heap_free(&k_heap_test, p);
173179
p = (char *)k_heap_alloc(&k_heap_test, ALLOC_SIZE_2, timeout);
174180
zassert_not_null(p, "k_heap_alloc operation failed");
175181
for (int i = 0; i < ALLOC_SIZE_2; i++) {
176182
p[i] = '0';
177183
}
178184
k_heap_free(&k_heap_test, p);
185+
186+
#ifndef CONFIG_ASSERT
187+
/* Double free: should not crash, but behavior is otherwise undefined */
188+
k_heap_free(&k_heap_test, p);
189+
#endif
179190
}
180191

181192
/**

0 commit comments

Comments
 (0)