Skip to content

Commit

Permalink
heap: add a unit test for malloc(0) and slightly optimize heap_caps_m…
Browse files Browse the repository at this point in the history
…alloc_prefer
  • Loading branch information
o-marshmallow committed Aug 9, 2022
1 parent ee4d15d commit c6ddf72
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components/heap/heap_caps.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ IRAM_ATTR void *heap_caps_malloc_prefer( size_t size, size_t num, ... )
while (num--) {
caps = va_arg( argp, uint32_t );
r = heap_caps_malloc_base( size, caps );
if (r != NULL) {
if (r != NULL || size == 0) {
break;
}
}
Expand Down
22 changes: 22 additions & 0 deletions components/heap/test/test_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,25 @@ TEST_CASE("malloc(0) should return a NULL pointer", "[heap]")
p = malloc(0);
TEST_ASSERT(p == NULL);
}

static bool failure_occured = false;

static void test_alloc_failure_callback(size_t size, uint32_t caps, const char * function_name)
{
failure_occured = true;
}

TEST_CASE("malloc/calloc(0) should not call failure callback", "[heap]")
{
void* ptr = NULL;
esp_err_t ret = heap_caps_register_failed_alloc_callback(test_alloc_failure_callback);
TEST_ASSERT(ret == ESP_OK);
ptr = malloc(0);
TEST_ASSERT_NULL(ptr);
/* Check that our callback was NOT called */
TEST_ASSERT_FALSE(failure_occured);
/* Do the same thing for calloc */
ptr = calloc(0, 0);
TEST_ASSERT_NULL(ptr);
TEST_ASSERT_FALSE(failure_occured);
}

0 comments on commit c6ddf72

Please sign in to comment.