comparison dbmalloc.c @ 1580:7f2be495dff6 coverity

merge coverity
author Matt Johnston <matt@ucc.asn.au>
date Sun, 04 Mar 2018 15:07:09 +0800
parents c42e8ff42bd1
children 1051e4eea25a
comparison
equal deleted inserted replaced
1545:0b991dec7ab9 1580:7f2be495dff6
1 #include "dbmalloc.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 */
61
62 struct dbmalloc_header {
63 unsigned int epoch;
64 struct dbmalloc_header *prev;
65 struct dbmalloc_header *next;
66 };
67
68 static void put_alloc(struct dbmalloc_header *header);
69 static void remove_alloc(struct dbmalloc_header *header);
70
71 /* end of the linked list */
72 static struct dbmalloc_header* staple;
73
74 unsigned int current_epoch = 0;
75
76 void m_malloc_set_epoch(unsigned int epoch) {
77 current_epoch = epoch;
78 }
79
80 void m_malloc_free_epoch(unsigned int epoch, int dofree) {
81 struct dbmalloc_header* header;
82 struct dbmalloc_header* nextheader = NULL;
83 struct dbmalloc_header* oldstaple = staple;
84 staple = NULL;
85 /* free allocations from this epoch, create a new staple-anchored list from
86 the remainder */
87 for (header = oldstaple; header; header = nextheader)
88 {
89 nextheader = header->next;
90 if (header->epoch == epoch) {
91 if (dofree) {
92 free(header);
93 }
94 } else {
95 header->prev = NULL;
96 header->next = NULL;
97 put_alloc(header);
98 }
99 }
100 }
101
102 static void put_alloc(struct dbmalloc_header *header) {
103 assert(header->next == NULL);
104 assert(header->prev == NULL);
105 if (staple) {
106 staple->prev = header;
107 }
108 header->next = staple;
109 staple = header;
110 }
111
112 static void remove_alloc(struct dbmalloc_header *header) {
113 if (header->prev) {
114 header->prev->next = header->next;
115 }
116 if (header->next) {
117 header->next->prev = header->prev;
118 }
119 if (staple == header) {
120 staple = header->next;
121 }
122 header->prev = NULL;
123 header->next = NULL;
124 }
125
126 static struct dbmalloc_header* get_header(void* ptr) {
127 char* bptr = ptr;
128 return (struct dbmalloc_header*)&bptr[-sizeof(struct dbmalloc_header)];
129 }
130
131 void * m_malloc(size_t size) {
132 char* mem = NULL;
133 struct dbmalloc_header* header = NULL;
134
135 if (size == 0 || size > 1e9) {
136 dropbear_exit("m_malloc failed");
137 }
138
139 size = size + sizeof(struct dbmalloc_header);
140
141 mem = calloc(1, size);
142 if (mem == NULL) {
143 dropbear_exit("m_malloc failed");
144 }
145 header = (struct dbmalloc_header*)mem;
146 put_alloc(header);
147 header->epoch = current_epoch;
148 return &mem[sizeof(struct dbmalloc_header)];
149 }
150
151 void * m_realloc(void* ptr, size_t size) {
152 char* mem = NULL;
153 struct dbmalloc_header* header = NULL;
154 if (size == 0 || size > 1e9) {
155 dropbear_exit("m_realloc failed");
156 }
157
158 header = get_header(ptr);
159 remove_alloc(header);
160
161 size = size + sizeof(struct dbmalloc_header);
162 mem = realloc(header, size);
163 if (mem == NULL) {
164 dropbear_exit("m_realloc failed");
165 }
166
167 header = (struct dbmalloc_header*)mem;
168 put_alloc(header);
169 return &mem[sizeof(struct dbmalloc_header)];
170 }
171
172 void m_free_direct(void* ptr) {
173 struct dbmalloc_header* header = NULL;
174 if (!ptr) {
175 return;
176 }
177 header = get_header(ptr);
178 remove_alloc(header);
179 free(header);
180 }
181
182 #endif /* DROPBEAR_TRACKING_MALLOC */