line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
16659
|
|
|
|
|
|
static void *stdalloc__malloc(size_t len, const char *file, int line) |
11
|
|
|
|
|
|
|
{ |
12
|
|
|
|
|
|
|
void *ptr; |
13
|
|
|
|
|
|
|
|
14
|
16659
|
|
|
|
|
|
GIT_UNUSED(file); |
15
|
16659
|
|
|
|
|
|
GIT_UNUSED(line); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#ifdef GIT_DEBUG_STRICT_ALLOC |
18
|
|
|
|
|
|
|
if (!len) |
19
|
|
|
|
|
|
|
return NULL; |
20
|
|
|
|
|
|
|
#endif |
21
|
|
|
|
|
|
|
|
22
|
16659
|
|
|
|
|
|
ptr = malloc(len); |
23
|
|
|
|
|
|
|
|
24
|
16659
|
50
|
|
|
|
|
if (!ptr) |
25
|
0
|
|
|
|
|
|
git_error_set_oom(); |
26
|
|
|
|
|
|
|
|
27
|
16659
|
|
|
|
|
|
return ptr; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
71526
|
|
|
|
|
|
static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line) |
31
|
|
|
|
|
|
|
{ |
32
|
|
|
|
|
|
|
void *ptr; |
33
|
|
|
|
|
|
|
|
34
|
71526
|
|
|
|
|
|
GIT_UNUSED(file); |
35
|
71526
|
|
|
|
|
|
GIT_UNUSED(line); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#ifdef GIT_DEBUG_STRICT_ALLOC |
38
|
|
|
|
|
|
|
if (!elsize || !nelem) |
39
|
|
|
|
|
|
|
return NULL; |
40
|
|
|
|
|
|
|
#endif |
41
|
|
|
|
|
|
|
|
42
|
71526
|
|
|
|
|
|
ptr = calloc(nelem, elsize); |
43
|
|
|
|
|
|
|
|
44
|
71526
|
50
|
|
|
|
|
if (!ptr) |
45
|
0
|
|
|
|
|
|
git_error_set_oom(); |
46
|
|
|
|
|
|
|
|
47
|
71526
|
|
|
|
|
|
return ptr; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
35774
|
|
|
|
|
|
static char *stdalloc__strdup(const char *str, const char *file, int line) |
51
|
|
|
|
|
|
|
{ |
52
|
|
|
|
|
|
|
char *ptr; |
53
|
|
|
|
|
|
|
|
54
|
35774
|
|
|
|
|
|
GIT_UNUSED(file); |
55
|
35774
|
|
|
|
|
|
GIT_UNUSED(line); |
56
|
|
|
|
|
|
|
|
57
|
35774
|
|
|
|
|
|
ptr = strdup(str); |
58
|
|
|
|
|
|
|
|
59
|
35774
|
50
|
|
|
|
|
if (!ptr) |
60
|
0
|
|
|
|
|
|
git_error_set_oom(); |
61
|
|
|
|
|
|
|
|
62
|
35774
|
|
|
|
|
|
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
|
5343
|
50
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) || |
|
|
50
|
|
|
|
|
|
73
|
5343
|
|
|
|
|
|
!(ptr = stdalloc__malloc(alloclength, file, line))) |
74
|
0
|
|
|
|
|
|
return NULL; |
75
|
|
|
|
|
|
|
|
76
|
5343
|
50
|
|
|
|
|
if (length) |
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
|
1348
|
50
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) || |
|
|
50
|
|
|
|
|
|
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
|
52958
|
|
|
|
|
|
static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line) |
99
|
|
|
|
|
|
|
{ |
100
|
|
|
|
|
|
|
void *new_ptr; |
101
|
|
|
|
|
|
|
|
102
|
52958
|
|
|
|
|
|
GIT_UNUSED(file); |
103
|
52958
|
|
|
|
|
|
GIT_UNUSED(line); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
#ifdef GIT_DEBUG_STRICT_ALLOC |
106
|
|
|
|
|
|
|
if (!size) |
107
|
|
|
|
|
|
|
return NULL; |
108
|
|
|
|
|
|
|
#endif |
109
|
|
|
|
|
|
|
|
110
|
52958
|
|
|
|
|
|
new_ptr = realloc(ptr, size); |
111
|
|
|
|
|
|
|
|
112
|
52958
|
50
|
|
|
|
|
if (!new_ptr) |
113
|
0
|
|
|
|
|
|
git_error_set_oom(); |
114
|
|
|
|
|
|
|
|
115
|
52958
|
|
|
|
|
|
return new_ptr; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
22995
|
|
|
|
|
|
static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line) |
119
|
|
|
|
|
|
|
{ |
120
|
|
|
|
|
|
|
size_t newsize; |
121
|
|
|
|
|
|
|
|
122
|
22995
|
50
|
|
|
|
|
if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) |
|
|
50
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return NULL; |
124
|
|
|
|
|
|
|
|
125
|
22995
|
|
|
|
|
|
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
|
183793
|
|
|
|
|
|
static void stdalloc__free(void *ptr) |
134
|
|
|
|
|
|
|
{ |
135
|
183793
|
|
|
|
|
|
free(ptr); |
136
|
183793
|
|
|
|
|
|
} |
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
|
|
|
|
|
|
|
} |