2
2
3
3
#include <setjmp.h>
4
4
#include <signal.h>
5
+ #include <stdint.h>
5
6
#include <stdio.h>
6
7
#include <stdlib.h>
7
8
#include <string.h>
@@ -58,6 +59,12 @@ static jmp_buf env;
58
59
static volatile sig_atomic_t jmp_ready = false;
59
60
static bool time_limited = false;
60
61
62
+ /* For test_malloc and test_calloc */
63
+ typedef enum {
64
+ TEST_MALLOC ,
65
+ TEST_CALLOC ,
66
+ } alloc_t ;
67
+
61
68
/* Internal functions */
62
69
63
70
/* Should this allocation fail? */
@@ -115,17 +122,23 @@ static size_t *find_footer(block_element_t *b)
115
122
return p ;
116
123
}
117
124
118
- /* Implementation of application functions */
119
-
120
- void * test_malloc (size_t size )
125
+ static void * alloc (alloc_t alloc_type , size_t size )
121
126
{
122
127
if (noallocate_mode ) {
123
- report_event (MSG_FATAL , "Calls to malloc disallowed" );
128
+ char * msg_alloc_forbidden [] = {
129
+ "Calls to malloc are disallowed" ,
130
+ "Calls to calloc are disallowed" ,
131
+ };
132
+ report_event (MSG_FATAL , "%s" , msg_alloc_forbidden [alloc_type ]);
124
133
return NULL ;
125
134
}
126
135
127
136
if (fail_allocation ()) {
128
- report_event (MSG_WARN , "Malloc returning NULL" );
137
+ char * msg_alloc_failure [] = {
138
+ "Malloc returning NULL" ,
139
+ "Calloc returning NULL" ,
140
+ };
141
+ report_event (MSG_WARN , "%s" , msg_alloc_failure [alloc_type ]);
129
142
return NULL ;
130
143
}
131
144
@@ -142,7 +155,7 @@ void *test_malloc(size_t size)
142
155
new_block -> payload_size = size ;
143
156
* find_footer (new_block ) = MAGICFOOTER ;
144
157
void * p = (void * ) & new_block -> payload ;
145
- memset (p , FILLCHAR , size );
158
+ memset (p , ! alloc_type * FILLCHAR , size );
146
159
// cppcheck-suppress nullPointerRedundantCheck
147
160
new_block -> next = allocated ;
148
161
// cppcheck-suppress nullPointerRedundantCheck
@@ -156,16 +169,22 @@ void *test_malloc(size_t size)
156
169
return p ;
157
170
}
158
171
172
+ /* Implementation of application functions */
173
+
174
+ void * test_malloc (size_t size )
175
+ {
176
+ return alloc (TEST_MALLOC , size );
177
+ }
178
+
159
179
// cppcheck-suppress unusedFunction
160
180
void * test_calloc (size_t nelem , size_t elsize )
161
181
{
162
182
/* Reference: Malloc tutorial
163
183
* https://danluu.com/malloc-tutorial/
164
184
*/
165
- size_t size = nelem * elsize ; // TODO: check for overflow
166
- void * ptr = test_malloc (size );
167
- memset (ptr , 0 , size );
168
- return ptr ;
185
+ if (!nelem || !elsize || nelem > SIZE_MAX / elsize )
186
+ return NULL ;
187
+ return alloc (TEST_CALLOC , nelem * elsize );
169
188
}
170
189
171
190
void test_free (void * p )
0 commit comments