Debian Codegraph @

components/playlist/xspf/uriparser/UriMemory.c

raw symbols SCIP
1 /*
2 * uriparser - RFC 3986 URI parsing library
3 *
4 * Copyright (C) 2018, Weijia Song <[email protected]>
5 * Copyright (C) 2018, Sebastian Pipping <[email protected]>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. Neither the name of the copyright holder nor the names of
22 * its contributors may be used to endorse or promote products
23 * derived from this software without specific prior written
24 * permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
37 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /**
41 * @file UriMemory.c
42 * Holds memory manager implementation.
43 */
44
45 #include "UriConfig.h" /* for HAVE_REALLOCARRAY */
46
47 #ifdef HAVE_REALLOCARRAY
48 # ifndef _GNU_SOURCE
49 # define _GNU_SOURCE 1
50 # endif
51 # ifdef __NetBSD__
52 # define _OPENBSD_SOURCE 1
53 # endif
54 #endif
55
56 #include <errno.h>
57 #include <stdlib.h>
58
59 #ifndef URI_DOXYGEN
60 # include "UriMemory.h"
61 #endif
62
63 #define URI_MAX(a, b) (((a) > (b)) ? (a) : (b))
64
65 /* NOTE: This intends to mimic MALLOC_ALIGNMENT of glibc */
66 #define URI_MALLOC_ALIGNMENT URI_MAX(2 * sizeof(size_t), sizeof(long double))
67
68 #define URI_MALLOC_PADDING (URI_MALLOC_ALIGNMENT - sizeof(size_t))
69
70 #define URI_CHECK_ALLOC_OVERFLOW(total_size, nmemb, size) \
71 do { \
72 /* check for unsigned overflow */ \
73 if ((nmemb != 0) && (total_size / nmemb != size)) { \
74 errno = ENOMEM; \
75 return NULL; \
76 } \
77 } while (0)
78
79 static void * uriDefaultMalloc(UriMemoryManager * URI_UNUSED(memory), size_t size) {
80 return malloc(size);
81 }
82
83 static void * uriDefaultCalloc(UriMemoryManager * URI_UNUSED(memory), size_t nmemb,
84 size_t size) {
85 return calloc(nmemb, size);
86 }
87
88 static void * uriDefaultRealloc(UriMemoryManager * URI_UNUSED(memory), void * ptr,
89 size_t size) {
90 return realloc(ptr, size);
91 }
92
93 static void * uriDefaultReallocarray(UriMemoryManager * URI_UNUSED(memory), void * ptr,
94 size_t nmemb, size_t size) {
95 #ifdef HAVE_REALLOCARRAY
96 return reallocarray(ptr, nmemb, size);
97 #else
98 const size_t total_size = nmemb * size;
99
100 URI_CHECK_ALLOC_OVERFLOW(total_size, nmemb, size); /* may return */
101
102 return realloc(ptr, total_size);
103 #endif
104 }
105
106 static void uriDefaultFree(UriMemoryManager * URI_UNUSED(memory), void * ptr) {
107 free(ptr);
108 }
109
110 UriBool uriMemoryManagerIsComplete(const UriMemoryManager * memory) {
111 return (memory && memory->malloc && memory->calloc && memory->realloc
112 && memory->reallocarray && memory->free)
113 ? URI_TRUE
114 : URI_FALSE;
115 }
116
117 void * uriEmulateCalloc(UriMemoryManager * memory, size_t nmemb, size_t size) {
118 void * buffer;
119 const size_t total_size = nmemb * size;
120
121 if (memory == NULL) {
122 errno = EINVAL;
123 return NULL;
124 }
125
126 URI_CHECK_ALLOC_OVERFLOW(total_size, nmemb, size); /* may return */
127
128 buffer = memory->malloc(memory, total_size);
129 if (buffer == NULL) {
130 /* errno set by malloc */
131 return NULL;
132 }
133 memset(buffer, 0, total_size);
134 return buffer;
135 }
136
137 void * uriEmulateReallocarray(UriMemoryManager * memory, void * ptr, size_t nmemb,
138 size_t size) {
139 const size_t total_size = nmemb * size;
140
141 if (memory == NULL) {
142 errno = EINVAL;
143 return NULL;
144 }
145
146 URI_CHECK_ALLOC_OVERFLOW(total_size, nmemb, size); /* may return */
147
148 return memory->realloc(memory, ptr, total_size);
149 }
150
151 static void * uriDecorateMalloc(UriMemoryManager * memory, size_t size) {
152 UriMemoryManager * backend;
153 const size_t extraBytes = sizeof(size_t) + URI_MALLOC_PADDING;
154 void * buffer;
155
156 if (memory == NULL) {
157 errno = EINVAL;
158 return NULL;
159 }
160
161 /* check for unsigned overflow */
162 if (size > ((size_t)-1) - extraBytes) {
163 errno = ENOMEM;
164 return NULL;
165 }
166
167 backend = (UriMemoryManager *)memory->userData;
168 if (backend == NULL) {
169 errno = EINVAL;
170 return NULL;
171 }
172
173 buffer = backend->malloc(backend, extraBytes + size);
174 if (buffer == NULL) {
175 return NULL;
176 }
177
178 *(size_t *)buffer = size;
179
180 return (char *)buffer + extraBytes;
181 }
182
183 static void * uriDecorateRealloc(UriMemoryManager * memory, void * ptr, size_t size) {
184 void * newBuffer;
185 size_t prevSize;
186
187 if (memory == NULL) {
188 errno = EINVAL;
189 return NULL;
190 }
191
192 /* man realloc: "If ptr is NULL, then the call is equivalent to
193 * malloc(size), for *all* values of size" */
194 if (ptr == NULL) {
195 return memory->malloc(memory, size);
196 }
197
198 /* man realloc: "If size is equal to zero, and ptr is *not* NULL,
199 * then the call is equivalent to free(ptr)." */
200 if (size == 0) {
201 memory->free(memory, ptr);
202 return NULL;
203 }
204
205 prevSize = *((size_t *)((char *)ptr - sizeof(size_t) - URI_MALLOC_PADDING));
206
207 /* Anything to do? */
208 /* mull-ignore-next cxx_le_to_lt */
209 if (size <= prevSize) {
210 return ptr;
211 }
212
213 newBuffer = memory->malloc(memory, size);
214 if (newBuffer == NULL) {
215 /* errno set by malloc */
216 return NULL;
217 }
218
219 memcpy(newBuffer, ptr, prevSize);
220
221 memory->free(memory, ptr);
222
223 return newBuffer;
224 }
225
226 static void uriDecorateFree(UriMemoryManager * memory, void * ptr) {
227 UriMemoryManager * backend;
228
229 if ((ptr == NULL) || (memory == NULL)) {
230 return;
231 }
232
233 backend = (UriMemoryManager *)memory->userData;
234 if (backend == NULL) {
235 return;
236 }
237
238 backend->free(backend, (char *)ptr - sizeof(size_t) - URI_MALLOC_PADDING);
239 }
240
241 int uriCompleteMemoryManager(UriMemoryManager * memory, UriMemoryManager * backend) {
242 if ((memory == NULL) || (backend == NULL)) {
243 return URI_ERROR_NULL;
244 }
245
246 if ((backend->malloc == NULL) || (backend->free == NULL)) {
247 return URI_ERROR_MEMORY_MANAGER_INCOMPLETE;
248 }
249
250 memory->calloc = uriEmulateCalloc;
251 memory->reallocarray = uriEmulateReallocarray;
252
253 memory->malloc = uriDecorateMalloc;
254 memory->realloc = uriDecorateRealloc;
255 memory->free = uriDecorateFree;
256
257 memory->userData = backend;
258
259 return URI_SUCCESS;
260 }
261
262 /* mull-off */
263 int uriTestMemoryManagerEx(UriMemoryManager * memory, UriBool challengeAlignment) {
264 const size_t mallocSize = 7;
265 const size_t callocNmemb = 3;
266 const size_t callocSize = 5;
267 const size_t callocTotalSize = callocNmemb * callocSize;
268 const size_t reallocSize = 11;
269 const size_t reallocarrayNmemb = 5;
270 const size_t reallocarraySize = 7;
271 const size_t reallocarrayTotal = reallocarrayNmemb * reallocarraySize;
272 size_t index;
273 char * buffer;
274
275 if (memory == NULL) {
276 return URI_ERROR_NULL;
277 }
278
279 if (uriMemoryManagerIsComplete(memory) != URI_TRUE) {
280 return URI_ERROR_MEMORY_MANAGER_INCOMPLETE;
281 }
282
283 /* malloc + free*/
284 buffer = memory->malloc(memory, mallocSize);
285 if (buffer == NULL) {
286 return URI_ERROR_MEMORY_MANAGER_FAULTY;
287 }
288 buffer[mallocSize - 1] = '\xF1';
289 memory->free(memory, buffer);
290 buffer = NULL;
291
292 /* calloc + free */
293 buffer = memory->calloc(memory, callocNmemb, callocSize);
294 if (buffer == NULL) {
295 return URI_ERROR_MEMORY_MANAGER_FAULTY;
296 }
297 for (index = 0; index < callocTotalSize; index++) { /* all zeros? */
298 if (buffer[index] != '\0') {
299 return URI_ERROR_MEMORY_MANAGER_FAULTY;
300 }
301 }
302 buffer[callocTotalSize - 1] = '\xF2';
303 memory->free(memory, buffer);
304 buffer = NULL;
305
306 /* malloc + realloc + free */
307 buffer = memory->malloc(memory, mallocSize);
308 if (buffer == NULL) {
309 return URI_ERROR_MEMORY_MANAGER_FAULTY;
310 }
311 for (index = 0; index < mallocSize; index++) {
312 buffer[index] = '\xF3';
313 }
314 buffer = memory->realloc(memory, buffer, reallocSize);
315 if (buffer == NULL) {
316 return URI_ERROR_MEMORY_MANAGER_FAULTY;
317 }
318 for (index = 0; index < mallocSize; index++) { /* previous content? */
319 if (buffer[index] != '\xF3') {
320 return URI_ERROR_MEMORY_MANAGER_FAULTY;
321 }
322 }
323 buffer[reallocSize - 1] = '\xF4';
324 memory->free(memory, buffer);
325 buffer = NULL;
326
327 /* malloc + realloc ptr!=NULL size==0 (equals free) */
328 buffer = memory->malloc(memory, mallocSize);
329 if (buffer == NULL) {
330 return URI_ERROR_MEMORY_MANAGER_FAULTY;
331 }
332 buffer[mallocSize - 1] = '\xF5';
333 memory->realloc(memory, buffer, 0);
334 buffer = NULL;
335
336 /* realloc ptr==NULL size!=0 (equals malloc) + free */
337 buffer = memory->realloc(memory, NULL, mallocSize);
338 if (buffer == NULL) {
339 return URI_ERROR_MEMORY_MANAGER_FAULTY;
340 }
341 buffer[mallocSize - 1] = '\xF6';
342 memory->free(memory, buffer);
343 buffer = NULL;
344
345 /* realloc ptr==NULL size==0 (equals malloc) + free */
346 buffer = memory->realloc(memory, NULL, 0);
347 if (buffer != NULL) {
348 memory->free(memory, buffer);
349 buffer = NULL;
350 }
351
352 /* malloc + reallocarray + free */
353 buffer = memory->malloc(memory, mallocSize);
354 if (buffer == NULL) {
355 return URI_ERROR_MEMORY_MANAGER_FAULTY;
356 }
357 for (index = 0; index < mallocSize; index++) {
358 buffer[index] = '\xF7';
359 }
360 buffer = memory->reallocarray(memory, buffer, reallocarrayNmemb, reallocarraySize);
361 if (buffer == NULL) {
362 return URI_ERROR_MEMORY_MANAGER_FAULTY;
363 }
364 for (index = 0; index < mallocSize; index++) { /* previous content? */
365 if (buffer[index] != '\xF7') {
366 return URI_ERROR_MEMORY_MANAGER_FAULTY;
367 }
368 }
369 buffer[reallocarrayTotal - 1] = '\xF8';
370 memory->free(memory, buffer);
371 buffer = NULL;
372
373 /* malloc + reallocarray ptr!=NULL nmemb==0 size!=0 (equals free) */
374 buffer = memory->malloc(memory, mallocSize);
375 if (buffer == NULL) {
376 return URI_ERROR_MEMORY_MANAGER_FAULTY;
377 }
378 buffer[mallocSize - 1] = '\xF9';
379 memory->reallocarray(memory, buffer, 0, reallocarraySize);
380 buffer = NULL;
381
382 /* malloc + reallocarray ptr!=NULL nmemb!=0 size==0 (equals free) */
383 buffer = memory->malloc(memory, mallocSize);
384 if (buffer == NULL) {
385 return URI_ERROR_MEMORY_MANAGER_FAULTY;
386 }
387 buffer[mallocSize - 1] = '\xFA';
388 memory->reallocarray(memory, buffer, reallocarrayNmemb, 0);
389 buffer = NULL;
390
391 /* malloc + reallocarray ptr!=NULL nmemb==0 size==0 (equals free) */
392 buffer = memory->malloc(memory, mallocSize);
393 if (buffer == NULL) {
394 return URI_ERROR_MEMORY_MANAGER_FAULTY;
395 }
396 buffer[mallocSize - 1] = '\xFB';
397 memory->reallocarray(memory, buffer, 0, 0);
398 buffer = NULL;
399
400 /* reallocarray ptr==NULL nmemb!=0 size!=0 (equals malloc) + free */
401 buffer = memory->reallocarray(memory, NULL, callocNmemb, callocSize);
402 if (buffer == NULL) {
403 return URI_ERROR_MEMORY_MANAGER_FAULTY;
404 }
405 buffer[callocTotalSize - 1] = '\xFC';
406 memory->free(memory, buffer);
407 buffer = NULL;
408
409 /* reallocarray ptr==NULL nmemb==0 size!=0 (equals malloc) + free */
410 buffer = memory->reallocarray(memory, NULL, 0, callocSize);
411 if (buffer != NULL) {
412 memory->free(memory, buffer);
413 buffer = NULL;
414 }
415
416 /* reallocarray ptr==NULL nmemb!=0 size==0 (equals malloc) + free */
417 buffer = memory->reallocarray(memory, NULL, callocNmemb, 0);
418 if (buffer != NULL) {
419 memory->free(memory, buffer);
420 buffer = NULL;
421 }
422
423 /* reallocarray ptr==NULL nmemb==0 size==0 (equals malloc) + free */
424 buffer = memory->reallocarray(memory, NULL, 0, 0);
425 if (buffer != NULL) {
426 memory->free(memory, buffer);
427 buffer = NULL;
428 }
429
430 /* challenge pointer alignment */
431 if (challengeAlignment == URI_TRUE) {
432 long double * ptr = memory->malloc(memory, 4 * sizeof(long double));
433 if (ptr != NULL) {
434 ptr[0] = 0.0L;
435 ptr[1] = 1.1L;
436 ptr[2] = 2.2L;
437 ptr[3] = 3.3L;
438
439 long double * const ptrNew =
440 memory->realloc(memory, ptr, 8 * sizeof(long double));
441 if (ptrNew != NULL) {
442 ptr = ptrNew;
443 ptr[4] = 4.4L;
444 ptr[5] = 5.5L;
445 ptr[6] = 6.6L;
446 ptr[7] = 7.7L;
447 }
448
449 memory->free(memory, ptr);
450 }
451 }
452
453 return URI_SUCCESS;
454 }
455 /* mull-on */
456
457 int uriTestMemoryManager(UriMemoryManager * memory) {
458 return uriTestMemoryManagerEx(memory, /*challengeAlignment=*/URI_FALSE);
459 }
460
461 /*extern*/ UriMemoryManager defaultMemoryManager = {
462 uriDefaultMalloc, uriDefaultCalloc, uriDefaultRealloc,
463 uriDefaultReallocarray, uriDefaultFree, NULL /* userData */
464 };