comparison dbmalloc.c @ 1569:c42e8ff42bd1

Only use malloc wrapper if fuzzing
author Matt Johnston <matt@ucc.asn.au>
date Thu, 01 Mar 2018 23:46:24 +0800
parents 7209a6e30932
children 1051e4eea25a
comparison
equal deleted inserted replaced
1568:119a459b00d0 1569:c42e8ff42bd1
1 #include "dbmalloc.h" 1 #include "dbmalloc.h"
2 #include "dbutil.h" 2 #include "dbutil.h"
3
4
5 void * m_calloc(size_t nmemb, size_t size) {
6 if (SIZE_T_MAX / nmemb < size) {
7 dropbear_exit("m_calloc failed");
8 }
9 return m_malloc(nmemb*size);
10 }
11
12 void * m_strdup(const char * str) {
13 char* ret;
14 unsigned int len;
15 len = strlen(str);
16
17 ret = m_malloc(len+1);
18 if (ret == NULL) {
19 dropbear_exit("m_strdup failed");
20 }
21 memcpy(ret, str, len+1);
22 return ret;
23 }
24
25 #if !DROPBEAR_TRACKING_MALLOC
26
27 /* Simple wrappers around malloc etc */
28 void * m_malloc(size_t size) {
29
30 void* ret;
31
32 if (size == 0) {
33 dropbear_exit("m_malloc failed");
34 }
35 ret = calloc(1, size);
36 if (ret == NULL) {
37 dropbear_exit("m_malloc failed");
38 }
39 return ret;
40
41 }
42
43 void * m_realloc(void* ptr, size_t size) {
44
45 void *ret;
46
47 if (size == 0) {
48 dropbear_exit("m_realloc failed");
49 }
50 ret = realloc(ptr, size);
51 if (ret == NULL) {
52 dropbear_exit("m_realloc failed");
53 }
54 return ret;
55 }
56
57
58 #else
59
60 /* For fuzzing */
3 61
4 struct dbmalloc_header { 62 struct dbmalloc_header {
5 unsigned int epoch; 63 unsigned int epoch;
6 struct dbmalloc_header *prev; 64 struct dbmalloc_header *prev;
7 struct dbmalloc_header *next; 65 struct dbmalloc_header *next;
88 put_alloc(header); 146 put_alloc(header);
89 header->epoch = current_epoch; 147 header->epoch = current_epoch;
90 return &mem[sizeof(struct dbmalloc_header)]; 148 return &mem[sizeof(struct dbmalloc_header)];
91 } 149 }
92 150
93 void * m_calloc(size_t nmemb, size_t size) {
94 if (SIZE_T_MAX / nmemb < size) {
95 dropbear_exit("m_calloc failed");
96 }
97 return m_malloc(nmemb*size);
98 }
99
100 void * m_realloc(void* ptr, size_t size) { 151 void * m_realloc(void* ptr, size_t size) {
101 char* mem = NULL; 152 char* mem = NULL;
102 struct dbmalloc_header* header = NULL; 153 struct dbmalloc_header* header = NULL;
103 if (size == 0 || size > 1e9) { 154 if (size == 0 || size > 1e9) {
104 dropbear_exit("m_realloc failed"); 155 dropbear_exit("m_realloc failed");
126 header = get_header(ptr); 177 header = get_header(ptr);
127 remove_alloc(header); 178 remove_alloc(header);
128 free(header); 179 free(header);
129 } 180 }
130 181
131 void * m_strdup(const char * str) { 182 #endif /* DROPBEAR_TRACKING_MALLOC */
132 char* ret;
133 unsigned int len;
134 len = strlen(str);
135
136 ret = m_malloc(len+1);
137 if (ret == NULL) {
138 dropbear_exit("m_strdup failed");
139 }
140 memcpy(ret, str, len+1);
141 return ret;
142 }
143
144