source src/allocators/stdalloc.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
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 | 923092 | 2 | static void *stdalloc__malloc(size_t len, const char *file, int line) | |
11 | - | { | ||
12 | 923092 | 2 | void *ptr = malloc(len); | |
13 | - | |||
14 | - | GIT_UNUSED(file); | ||
15 | - | GIT_UNUSED(line); | ||
16 | - | |||
17 | 923092 | 2,3 | if (!ptr) git_error_set_oom(); | |
18 | 923937 | 4 | return ptr; | |
19 | - | } | ||
20 | - | |||
21 | 1959265 | 2 | static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line) | |
22 | - | { | ||
23 | 1959265 | 2 | void *ptr = calloc(nelem, elsize); | |
24 | - | |||
25 | - | GIT_UNUSED(file); | ||
26 | - | GIT_UNUSED(line); | ||
27 | - | |||
28 | 1959265 | 2,3 | if (!ptr) git_error_set_oom(); | |
29 | 1964749 | 4 | return ptr; | |
30 | - | } | ||
31 | - | |||
32 | 825666 | 2 | static char *stdalloc__strdup(const char *str, const char *file, int line) | |
33 | - | { | ||
34 | 825666 | 2 | char *ptr = strdup(str); | |
35 | - | |||
36 | - | GIT_UNUSED(file); | ||
37 | - | GIT_UNUSED(line); | ||
38 | - | |||
39 | 825666 | 2,3 | if (!ptr) git_error_set_oom(); | |
40 | 825832 | 4 | return ptr; | |
41 | - | } | ||
42 | - | |||
43 | - | 2 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files)static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line) | |
44 | - | { | ||
45 | - | 2 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) size_t length = 0, alloclength; | |
46 | - | char *ptr; | ||
47 | - | |||
48 | - | 2 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) length = p_strnlen(str, n); | |
49 | - | |||
50 | - | 3-7 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) || | |
51 | - | 6 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) !(ptr = stdalloc__malloc(alloclength, file, line))) | |
52 | - | 8 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) return NULL; | |
53 | - | |||
54 | - | 9 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) if (length) | |
55 | - | 10 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) memcpy(ptr, str, length); | |
56 | - | |||
57 | - | 11 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) ptr[length] = '\0'; | |
58 | - | |||
59 | - | 11 | suppressed: function cannot be solved stdalloc__strndup (automatic due to inconsistent arc counts in .gcda files) return ptr; | |
60 | - | } | ||
61 | - | |||
62 | 52638 | 2 | 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 | 52638 | 2-6 | if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) || | |
68 | 52638 | 5 | !(ptr = stdalloc__malloc(alloclen, file, line))) | |
69 | ##### | 7 | return NULL; | |
70 | - | |||
71 | 52638 | 8 | memcpy(ptr, start, n); | |
72 | 52638 | 8 | ptr[n] = '\0'; | |
73 | 52638 | 8 | return ptr; | |
74 | - | } | ||
75 | - | |||
76 | 2768150 | 2 | static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line) | |
77 | - | { | ||
78 | 2768150 | 2 | void *new_ptr = realloc(ptr, size); | |
79 | - | |||
80 | - | GIT_UNUSED(file); | ||
81 | - | GIT_UNUSED(line); | ||
82 | - | |||
83 | 2768150 | 2,3 | if (!new_ptr) git_error_set_oom(); | |
84 | 2777989 | 4 | return new_ptr; | |
85 | - | } | ||
86 | - | |||
87 | 939889 | 2 | static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line) | |
88 | - | { | ||
89 | - | size_t newsize; | ||
90 | - | |||
91 | 939889 | 2-7 | if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize)) | |
92 | ##### | 8 | return NULL; | |
93 | - | |||
94 | 939746 | 9 | return stdalloc__realloc(ptr, newsize, file, line); | |
95 | - | } | ||
96 | - | |||
97 | 142 | 2 | static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line) | |
98 | - | { | ||
99 | 142 | 2 | return stdalloc__reallocarray(NULL, nelem, elsize, file, line); | |
100 | - | } | ||
101 | - | |||
102 | 7376125 | 2 | static void stdalloc__free(void *ptr) | |
103 | - | { | ||
104 | 7376125 | 2 | free(ptr); | |
105 | 7376125 | 2 | } | |
106 | - | |||
107 | 14 | 2 | int git_stdalloc_init_allocator(git_allocator *allocator) | |
108 | - | { | ||
109 | 14 | 2 | allocator->gmalloc = stdalloc__malloc; | |
110 | 14 | 2 | allocator->gcalloc = stdalloc__calloc; | |
111 | 14 | 2 | allocator->gstrdup = stdalloc__strdup; | |
112 | 14 | 2 | allocator->gstrndup = stdalloc__strndup; | |
113 | 14 | 2 | allocator->gsubstrdup = stdalloc__substrdup; | |
114 | 14 | 2 | allocator->grealloc = stdalloc__realloc; | |
115 | 14 | 2 | allocator->greallocarray = stdalloc__reallocarray; | |
116 | 14 | 2 | allocator->gmallocarray = stdalloc__mallocarray; | |
117 | 14 | 2 | allocator->gfree = stdalloc__free; | |
118 | 14 | 2 | return 0; | |
119 | - | } |