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
|
15911
|
|
|
|
|
|
static void *stdalloc__malloc(size_t len, const char *file, int line) |
11
|
|
|
|
|
|
|
{ |
12
|
15911
|
|
|
|
|
|
void *ptr = malloc(len); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
GIT_UNUSED(file); |
15
|
|
|
|
|
|
|
GIT_UNUSED(line); |
16
|
|
|
|
|
|
|
|
17
|
15911
|
50
|
|
|
|
|
if (!ptr) git_error_set_oom(); |
18
|
15911
|
|
|
|
|
|
return ptr; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
48662
|
|
|
|
|
|
static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line) |
22
|
|
|
|
|
|
|
{ |
23
|
48662
|
|
|
|
|
|
void *ptr = calloc(nelem, elsize); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
GIT_UNUSED(file); |
26
|
|
|
|
|
|
|
GIT_UNUSED(line); |
27
|
|
|
|
|
|
|
|
28
|
48662
|
50
|
|
|
|
|
if (!ptr) git_error_set_oom(); |
29
|
48662
|
|
|
|
|
|
return ptr; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
31612
|
|
|
|
|
|
static char *stdalloc__strdup(const char *str, const char *file, int line) |
33
|
|
|
|
|
|
|
{ |
34
|
31612
|
|
|
|
|
|
char *ptr = strdup(str); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
GIT_UNUSED(file); |
37
|
|
|
|
|
|
|
GIT_UNUSED(line); |
38
|
|
|
|
|
|
|
|
39
|
31612
|
50
|
|
|
|
|
if (!ptr) git_error_set_oom(); |
40
|
31612
|
|
|
|
|
|
return ptr; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
4452
|
|
|
|
|
|
static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line) |
44
|
|
|
|
|
|
|
{ |
45
|
4452
|
|
|
|
|
|
size_t length = 0, alloclength; |
46
|
|
|
|
|
|
|
char *ptr; |
47
|
|
|
|
|
|
|
|
48
|
4452
|
|
|
|
|
|
length = p_strnlen(str, n); |
49
|
|
|
|
|
|
|
|
50
|
4452
|
50
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) || |
|
|
50
|
|
|
|
|
|
51
|
4452
|
|
|
|
|
|
!(ptr = stdalloc__malloc(alloclength, file, line))) |
52
|
0
|
|
|
|
|
|
return NULL; |
53
|
|
|
|
|
|
|
|
54
|
4452
|
50
|
|
|
|
|
if (length) |
55
|
4452
|
|
|
|
|
|
memcpy(ptr, str, length); |
56
|
|
|
|
|
|
|
|
57
|
4452
|
|
|
|
|
|
ptr[length] = '\0'; |
58
|
|
|
|
|
|
|
|
59
|
4452
|
|
|
|
|
|
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
|
46376
|
|
|
|
|
|
static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line) |
77
|
|
|
|
|
|
|
{ |
78
|
46376
|
|
|
|
|
|
void *new_ptr = realloc(ptr, size); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
GIT_UNUSED(file); |
81
|
|
|
|
|
|
|
GIT_UNUSED(line); |
82
|
|
|
|
|
|
|
|
83
|
46376
|
50
|
|
|
|
|
if (!new_ptr) git_error_set_oom(); |
84
|
46376
|
|
|
|
|
|
return new_ptr; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
18603
|
|
|
|
|
|
static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line) |
88
|
|
|
|
|
|
|
{ |
89
|
|
|
|
|
|
|
size_t newsize; |
90
|
|
|
|
|
|
|
|
91
|
18603
|
50
|
|
|
|
|
if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) |
|
|
50
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
return NULL; |
93
|
|
|
|
|
|
|
|
94
|
18603
|
|
|
|
|
|
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
|
150884
|
|
|
|
|
|
static void stdalloc__free(void *ptr) |
103
|
|
|
|
|
|
|
{ |
104
|
150884
|
|
|
|
|
|
free(ptr); |
105
|
150884
|
|
|
|
|
|
} |
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
|
|
|
|
|
|
|
} |