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