Loading...
Searching...
No Matches
🧩 Dynamic Memory Allocation
📚 The CMocka API

Detect memory leaks, buffer overflows, and allocation errors. More...

## Functions

void * | test_malloc (size_t size)
| Test function overriding malloc.
void * | test_calloc (size_t nmemb, size_t size)
| Test function overriding calloc.
void * | test_realloc (void *ptr, size_t size)
| Test function overriding realloc which detects buffer overruns and memory leaks.
void | test_free (void *ptr)
| Test function overriding free(3).

## Detailed Description

Detect memory leaks, buffer overflows, and allocation errors.

Warning
  This shouldn't be used anymore, better use AddressSanitizer.

To test for memory leaks, buffer overflows and underflows a module being tested by cmocka should replace calls to malloc(), calloc() and free() to test_malloc(), test_calloc() and test_free() respectively. Each time a block is deallocated using test_free() it is checked for corruption, if a corrupt block is found a test failure is signalled. All blocks allocated using the test_*() allocation functions are tracked by the cmocka library. When a test completes if any allocated blocks (memory leaks) remain they are reported and a test failure is signalled.

For simplicity cmocka currently executes all tests in one process. Therefore all test cases in a test application share a single address space which means memory corruption from a single test case could potentially cause the test application to exit prematurely.

Automatic Allocation Redirection (Deprecated):

When both UNIT_TESTING and ALLOCATION_TESTING are defined, the standard C library allocation functions (malloc, calloc, realloc, free) are automatically redirected to cmocka's test allocators. This enables automatic memory leak detection and helps ensure proper memory management in tested code.

Note
  This feature is deprecated and should not be used in new code. Better use AddressSanitizer.

To enable allocation testing, define both macros before including cmocka.h:

#include <stdlib.h>
#define UNIT_TESTING 1
#define ALLOCATION_TESTING 1
#include <cmocka.h>
// This code will have malloc/free automatically redirected
void* ptr = malloc(100); // Actually calls test_malloc()
free(ptr); // Actually calls test_free()

With ALLOCATION_TESTING enabled:

- malloc() → test_malloc()
- calloc() → test_calloc()
- realloc() → test_realloc()
- free() → test_free()

Warning
  Mixing regular allocations with test allocations can lead to memory corruption. Ensure consistent usage throughout your test code.

## Function Documentation

## test_calloc()

void * test_calloc | ( | size_t | nmemb,
|  | size_t | size )

Test function overriding calloc.

The memory is set to zero.

Parameters

  [in] | nmemb | The number of elements for an array to be allocated.
  [in] | size | The size in bytes of each array element to allocate.

Returns
  A pointer to the allocated memory, NULL on error.

See also
  calloc(3)

## test_free()

void test_free | ( | void * | ptr | ) |

Test function overriding free(3).

Parameters

  [in] | ptr | The pointer to the memory space to free.

See also
  free(3).

## test_malloc()

void * test_malloc | ( | size_t | size | ) |

Test function overriding malloc.

Parameters

  [in] | size | The bytes which should be allocated.

Returns
  A pointer to the allocated memory or NULL on error.

#ifdef UNIT_TESTING
extern void* _test_malloc(const size_t size, const char* file, const int line);
#define malloc(size) _test_malloc(size, __FILE__, __LINE__)
#endif
void leak_memory() {
int * const temporary = (int*)malloc(sizeof(int));
*temporary = 0;
}

See also
  malloc(3)

## test_realloc()

void * test_realloc | ( | void * | ptr,
|  | size_t | size )

Test function overriding realloc which detects buffer overruns and memory leaks.

Parameters

  [in] | ptr | The memory block which should be changed.
  [in] | size | The bytes which should be allocated.

Returns
  The newly allocated memory block, NULL on error.