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
|
16388
|
|
|
|
|
|
static void *stdalloc__malloc(size_t len, const char *file, int line) |
11
|
|
|
|
|
|
|
{ |
12
|
16388
|
|
|
|
|
|
void *ptr = malloc(len); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
GIT_UNUSED(file); |
15
|
|
|
|
|
|
|
GIT_UNUSED(line); |
16
|
|
|
|
|
|
|
|
17
|
16388
|
50
|
|
|
|
|
if (!ptr) git_error_set_oom(); |
18
|
16388
|
|
|
|
|
|
return ptr; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
67332
|
|
|
|
|
|
static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line) |
22
|
|
|
|
|
|
|
{ |
23
|
67332
|
|
|
|
|
|
void *ptr = calloc(nelem, elsize); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
GIT_UNUSED(file); |
26
|
|
|
|
|
|
|
GIT_UNUSED(line); |
27
|
|
|
|
|
|
|
|
28
|
67332
|
50
|
|
|
|
|
if (!ptr) git_error_set_oom(); |
29
|
67332
|
|
|
|
|
|
return ptr; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
33034
|
|
|
|
|
|
static char *stdalloc__strdup(const char *str, const char *file, int line) |
33
|
|
|
|
|
|
|
{ |
34
|
33034
|
|
|
|
|
|
char *ptr = strdup(str); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
GIT_UNUSED(file); |
37
|
|
|
|
|
|
|
GIT_UNUSED(line); |
38
|
|
|
|
|
|
|
|
39
|
33034
|
50
|
|
|
|
|
if (!ptr) git_error_set_oom(); |
40
|
33034
|
|
|
|
|
|
return ptr; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
5137
|
|
|
|
|
|
static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line) |
44
|
|
|
|
|
|
|
{ |
45
|
5137
|
|
|
|
|
|
size_t length = 0, alloclength; |
46
|
|
|
|
|
|
|
char *ptr; |
47
|
|
|
|
|
|
|
|
48
|
5137
|
|
|
|
|
|
length = p_strnlen(str, n); |
49
|
|
|
|
|
|
|
|
50
|
5137
|
50
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) || |
|
|
50
|
|
|
|
|
|
51
|
5137
|
|
|
|
|
|
!(ptr = stdalloc__malloc(alloclength, file, line))) |
52
|
0
|
|
|
|
|
|
return NULL; |
53
|
|
|
|
|
|
|
|
54
|
5137
|
50
|
|
|
|
|
if (length) |
55
|
5137
|
|
|
|
|
|
memcpy(ptr, str, length); |
56
|
|
|
|
|
|
|
|
57
|
5137
|
|
|
|
|
|
ptr[length] = '\0'; |
58
|
|
|
|
|
|
|
|
59
|
5137
|
|
|
|
|
|
return ptr; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
1360
|
|
|
|
|
|
static char *stdalloc__substrdup(const char *start, size_t n, const char *file, int line) |
63
|
|
|
|
|
|
|
{ |
64
|
|
|
|
|
|
|
char *ptr; |
65
|
|
|
|
|
|
|
size_t alloclen; |
66
|
|
|
|
|
|
|
|
67
|
1360
|
50
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) || |
|
|
50
|
|
|
|
|
|
68
|
1360
|
|
|
|
|
|
!(ptr = stdalloc__malloc(alloclen, file, line))) |
69
|
0
|
|
|
|
|
|
return NULL; |
70
|
|
|
|
|
|
|
|
71
|
1360
|
|
|
|
|
|
memcpy(ptr, start, n); |
72
|
1360
|
|
|
|
|
|
ptr[n] = '\0'; |
73
|
1360
|
|
|
|
|
|
return ptr; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
49075
|
|
|
|
|
|
static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line) |
77
|
|
|
|
|
|
|
{ |
78
|
49075
|
|
|
|
|
|
void *new_ptr = realloc(ptr, size); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
GIT_UNUSED(file); |
81
|
|
|
|
|
|
|
GIT_UNUSED(line); |
82
|
|
|
|
|
|
|
|
83
|
49075
|
50
|
|
|
|
|
if (!new_ptr) git_error_set_oom(); |
84
|
49075
|
|
|
|
|
|
return new_ptr; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
19402
|
|
|
|
|
|
static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line) |
88
|
|
|
|
|
|
|
{ |
89
|
|
|
|
|
|
|
size_t newsize; |
90
|
|
|
|
|
|
|
|
91
|
19402
|
50
|
|
|
|
|
if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) |
|
|
50
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return NULL; |
93
|
|
|
|
|
|
|
|
94
|
19402
|
|
|
|
|
|
return stdalloc__realloc(ptr, newsize, file, line); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
13
|
|
|
|
|
|
static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line) |
98
|
|
|
|
|
|
|
{ |
99
|
13
|
|
|
|
|
|
return stdalloc__reallocarray(NULL, nelem, elsize, file, line); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
174810
|
|
|
|
|
|
static void stdalloc__free(void *ptr) |
103
|
|
|
|
|
|
|
{ |
104
|
174810
|
|
|
|
|
|
free(ptr); |
105
|
174810
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
86
|
|
|
|
|
|
int git_stdalloc_init_allocator(git_allocator *allocator) |
108
|
|
|
|
|
|
|
{ |
109
|
86
|
|
|
|
|
|
allocator->gmalloc = stdalloc__malloc; |
110
|
86
|
|
|
|
|
|
allocator->gcalloc = stdalloc__calloc; |
111
|
86
|
|
|
|
|
|
allocator->gstrdup = stdalloc__strdup; |
112
|
86
|
|
|
|
|
|
allocator->gstrndup = stdalloc__strndup; |
113
|
86
|
|
|
|
|
|
allocator->gsubstrdup = stdalloc__substrdup; |
114
|
86
|
|
|
|
|
|
allocator->grealloc = stdalloc__realloc; |
115
|
86
|
|
|
|
|
|
allocator->greallocarray = stdalloc__reallocarray; |
116
|
86
|
|
|
|
|
|
allocator->gmallocarray = stdalloc__mallocarray; |
117
|
86
|
|
|
|
|
|
allocator->gfree = stdalloc__free; |
118
|
86
|
|
|
|
|
|
return 0; |
119
|
|
|
|
|
|
|
} |