LCOV - code coverage report
Current view: top level - libs/testmocking/src - testmocking_malloc.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 11 11
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 3 3
Branches: 100.0 % 2 2

             Branch data     Line data    Source code
       1                 :             : #include "testmocking.h"
       2                 :             : 
       3                 :             : #include <stdlib.h>
       4                 :             : 
       5                 :             : static size_t testmocking_malloc_remaining = 0;
       6                 :             : 
       7                 :             : /**
       8                 :             :  * @brief Arm malloc mock to return NULL for the next n calls
       9                 :             :  *
      10                 :             :  * @param[in] n Number of consecutive malloc calls that must return NULL
      11                 :             :  */
      12                 :           1 : void testmocking_malloc_fail_next(size_t n)
      13                 :             : {
      14                 :           1 :         testmocking_malloc_remaining = n;
      15                 :           1 : }
      16                 :             : 
      17                 :             : /**
      18                 :             :  * @brief Disable malloc mock unconditionally
      19                 :             :  */
      20                 :           1 : void testmocking_malloc_disable(void)
      21                 :             : {
      22                 :           1 :         testmocking_malloc_remaining = 0;
      23                 :           1 : }
      24                 :             : 
      25                 :             : #ifndef EVIL_EMPIRE_OS
      26                 :             : void *__real_malloc(size_t size);
      27                 :             : 
      28                 :             : /**
      29                 :             :  * @brief Linker-redirected entry point for malloc
      30                 :             :  *
      31                 :             :  * When the mock is armed each call decrements the remaining counter and
      32                 :             :  * returns NULL. When the counter reaches zero the call is forwarded to libc
      33                 :             :  *
      34                 :             :  * @param[in] size Allocation size in bytes
      35                 :             :  * @return Pointer returned by libc malloc, or NULL when the mock fires
      36                 :             :  */
      37                 :      317510 : void *__wrap_malloc(size_t size)
      38                 :             : {
      39         [ +  + ]:      317510 :         if(testmocking_malloc_remaining > 0)
      40                 :             :         {
      41                 :           1 :                 testmocking_malloc_remaining--;
      42                 :           1 :                 return NULL;
      43                 :             :         }
      44                 :             : 
      45                 :      317509 :         return __real_malloc(size);
      46                 :             : }
      47                 :             : #endif
        

Generated by: LCOV version 2.0-1