| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "stdalloc.h" |
| 9 |
|
| |
| 10 |
16691 |
| static void *stdalloc__malloc(size_t len, const char *file, int line) |
| 11 |
|
| { |
| 12 |
|
| void *ptr; |
| 13 |
|
| |
| 14 |
16691 |
| GIT_UNUSED(file); |
| 15 |
16691 |
| GIT_UNUSED(line); |
| 16 |
|
| |
| 17 |
|
| #ifdef GIT_DEBUG_STRICT_ALLOC |
| 18 |
|
| if (!len) |
| 19 |
|
| return NULL; |
| 20 |
|
| #endif |
| 21 |
|
| |
| 22 |
16691 |
| ptr = malloc(len); |
| 23 |
|
| |
| 24 |
16691 |
▶ | if (!ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ptr) |
0 |
16691 |
16691 |
|
| 25 |
0 |
| git_error_set_oom(); |
| 26 |
|
| |
| 27 |
16691 |
| return ptr; |
| 28 |
|
| } |
| 29 |
|
| |
| 30 |
72126 |
| static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line) |
| 31 |
|
| { |
| 32 |
|
| void *ptr; |
| 33 |
|
| |
| 34 |
72126 |
| GIT_UNUSED(file); |
| 35 |
72126 |
| GIT_UNUSED(line); |
| 36 |
|
| |
| 37 |
|
| #ifdef GIT_DEBUG_STRICT_ALLOC |
| 38 |
|
| if (!elsize || !nelem) |
| 39 |
|
| return NULL; |
| 40 |
|
| #endif |
| 41 |
|
| |
| 42 |
72126 |
| ptr = calloc(nelem, elsize); |
| 43 |
|
| |
| 44 |
72126 |
▶ | if (!ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ptr) |
0 |
72126 |
72126 |
|
| 45 |
0 |
| git_error_set_oom(); |
| 46 |
|
| |
| 47 |
72126 |
| return ptr; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
36062 |
| static char *stdalloc__strdup(const char *str, const char *file, int line) |
| 51 |
|
| { |
| 52 |
|
| char *ptr; |
| 53 |
|
| |
| 54 |
36062 |
| GIT_UNUSED(file); |
| 55 |
36062 |
| GIT_UNUSED(line); |
| 56 |
|
| |
| 57 |
36062 |
| ptr = strdup(str); |
| 58 |
|
| |
| 59 |
36062 |
▶ | if (!ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ptr) |
0 |
36062 |
36062 |
|
| 60 |
0 |
| git_error_set_oom(); |
| 61 |
|
| |
| 62 |
36062 |
| return ptr; |
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
5343 |
| static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line) |
| 66 |
|
| { |
| 67 |
5343 |
| size_t length = 0, alloclength; |
| 68 |
|
| char *ptr; |
| 69 |
|
| |
| 70 |
5343 |
| length = p_strnlen(str, n); |
| 71 |
|
| |
| 72 |
10686 |
▶ | if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) || |
0 |
5343 |
5343 |
| if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) || |
0 |
5343 |
5343 |
|
| 73 |
5343 |
| !(ptr = stdalloc__malloc(alloclength, file, line))) |
| 74 |
0 |
| return NULL; |
| 75 |
|
| |
| 76 |
5343 |
▶ | if (length) |
Branch: 1 branch
| Branch | True | False | Total |
| if (length) |
5343 |
0 |
5343 |
|
| 77 |
5343 |
| memcpy(ptr, str, length); |
| 78 |
|
| |
| 79 |
5343 |
| ptr[length] = '\0'; |
| 80 |
|
| |
| 81 |
5343 |
| return ptr; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
1348 |
| static char *stdalloc__substrdup(const char *start, size_t n, const char *file, int line) |
| 85 |
|
| { |
| 86 |
|
| char *ptr; |
| 87 |
|
| size_t alloclen; |
| 88 |
|
| |
| 89 |
2696 |
▶ | if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) || |
0 |
1348 |
1348 |
| if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) || |
0 |
1348 |
1348 |
|
| 90 |
1348 |
| !(ptr = stdalloc__malloc(alloclen, file, line))) |
| 91 |
0 |
| return NULL; |
| 92 |
|
| |
| 93 |
1348 |
| memcpy(ptr, start, n); |
| 94 |
1348 |
| ptr[n] = '\0'; |
| 95 |
1348 |
| return ptr; |
| 96 |
|
| } |
| 97 |
|
| |
| 98 |
54417 |
| static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line) |
| 99 |
|
| { |
| 100 |
|
| void *new_ptr; |
| 101 |
|
| |
| 102 |
54417 |
| GIT_UNUSED(file); |
| 103 |
54417 |
| GIT_UNUSED(line); |
| 104 |
|
| |
| 105 |
|
| #ifdef GIT_DEBUG_STRICT_ALLOC |
| 106 |
|
| if (!size) |
| 107 |
|
| return NULL; |
| 108 |
|
| #endif |
| 109 |
|
| |
| 110 |
54417 |
| new_ptr = realloc(ptr, size); |
| 111 |
|
| |
| 112 |
54417 |
▶ | if (!new_ptr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_ptr) |
0 |
54417 |
54417 |
|
| 113 |
0 |
| git_error_set_oom(); |
| 114 |
|
| |
| 115 |
54417 |
| return new_ptr; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
23173 |
| static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line) |
| 119 |
|
| { |
| 120 |
|
| size_t newsize; |
| 121 |
|
| |
| 122 |
23173 |
▶ | if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) |
0 |
23173 |
23173 |
| if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) |
0 |
23173 |
23173 |
|
| 123 |
0 |
| return NULL; |
| 124 |
|
| |
| 125 |
23173 |
| return stdalloc__realloc(ptr, newsize, file, line); |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
15 |
| static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line) |
| 129 |
|
| { |
| 130 |
15 |
| return stdalloc__reallocarray(NULL, nelem, elsize, file, line); |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
185079 |
| static void stdalloc__free(void *ptr) |
| 134 |
|
| { |
| 135 |
185079 |
| free(ptr); |
| 136 |
185079 |
| } |
| 137 |
|
| |
| 138 |
87 |
| int git_stdalloc_init_allocator(git_allocator *allocator) |
| 139 |
|
| { |
| 140 |
87 |
| allocator->gmalloc = stdalloc__malloc; |
| 141 |
87 |
| allocator->gcalloc = stdalloc__calloc; |
| 142 |
87 |
| allocator->gstrdup = stdalloc__strdup; |
| 143 |
87 |
| allocator->gstrndup = stdalloc__strndup; |
| 144 |
87 |
| allocator->gsubstrdup = stdalloc__substrdup; |
| 145 |
87 |
| allocator->grealloc = stdalloc__realloc; |
| 146 |
87 |
| allocator->greallocarray = stdalloc__reallocarray; |
| 147 |
87 |
| allocator->gmallocarray = stdalloc__mallocarray; |
| 148 |
87 |
| allocator->gfree = stdalloc__free; |
| 149 |
87 |
| return 0; |
| 150 |
|
| } |