]> git.proxmox.com Git - libgit2.git/blame - src/util/allocators/stdalloc.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / src / util / allocators / stdalloc.c
CommitLineData
ac3d33df
JK
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
10static void *stdalloc__malloc(size_t len, const char *file, int line)
11{
c25aa7cd 12 void *ptr;
ac3d33df
JK
13
14 GIT_UNUSED(file);
15 GIT_UNUSED(line);
16
c25aa7cd
PP
17#ifdef GIT_DEBUG_STRICT_ALLOC
18 if (!len)
19 return NULL;
20#endif
21
22 ptr = malloc(len);
23
24 if (!ptr)
25 git_error_set_oom();
26
ac3d33df
JK
27 return ptr;
28}
29
30static void *stdalloc__calloc(size_t nelem, size_t elsize, const char *file, int line)
31{
c25aa7cd 32 void *ptr;
ac3d33df
JK
33
34 GIT_UNUSED(file);
35 GIT_UNUSED(line);
36
c25aa7cd
PP
37#ifdef GIT_DEBUG_STRICT_ALLOC
38 if (!elsize || !nelem)
39 return NULL;
40#endif
41
42 ptr = calloc(nelem, elsize);
43
44 if (!ptr)
45 git_error_set_oom();
46
ac3d33df
JK
47 return ptr;
48}
49
50static char *stdalloc__strdup(const char *str, const char *file, int line)
51{
c25aa7cd 52 char *ptr;
ac3d33df
JK
53
54 GIT_UNUSED(file);
55 GIT_UNUSED(line);
56
c25aa7cd
PP
57 ptr = strdup(str);
58
59 if (!ptr)
60 git_error_set_oom();
61
ac3d33df
JK
62 return ptr;
63}
64
65static char *stdalloc__strndup(const char *str, size_t n, const char *file, int line)
66{
67 size_t length = 0, alloclength;
68 char *ptr;
69
70 length = p_strnlen(str, n);
71
72 if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
c25aa7cd 73 !(ptr = stdalloc__malloc(alloclength, file, line)))
ac3d33df
JK
74 return NULL;
75
76 if (length)
77 memcpy(ptr, str, length);
78
79 ptr[length] = '\0';
80
81 return ptr;
82}
83
84static char *stdalloc__substrdup(const char *start, size_t n, const char *file, int line)
85{
86 char *ptr;
87 size_t alloclen;
88
89 if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
c25aa7cd 90 !(ptr = stdalloc__malloc(alloclen, file, line)))
ac3d33df
JK
91 return NULL;
92
93 memcpy(ptr, start, n);
94 ptr[n] = '\0';
95 return ptr;
96}
97
98static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int line)
99{
c25aa7cd 100 void *new_ptr;
ac3d33df
JK
101
102 GIT_UNUSED(file);
103 GIT_UNUSED(line);
104
c25aa7cd
PP
105#ifdef GIT_DEBUG_STRICT_ALLOC
106 if (!size)
107 return NULL;
108#endif
109
110 new_ptr = realloc(ptr, size);
111
112 if (!new_ptr)
113 git_error_set_oom();
114
ac3d33df
JK
115 return new_ptr;
116}
117
118static void *stdalloc__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
119{
120 size_t newsize;
121
22a2d3d5
UG
122 if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize))
123 return NULL;
ac3d33df 124
22a2d3d5 125 return stdalloc__realloc(ptr, newsize, file, line);
ac3d33df
JK
126}
127
128static void *stdalloc__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
129{
130 return stdalloc__reallocarray(NULL, nelem, elsize, file, line);
131}
132
133static void stdalloc__free(void *ptr)
134{
135 free(ptr);
136}
137
138int git_stdalloc_init_allocator(git_allocator *allocator)
139{
140 allocator->gmalloc = stdalloc__malloc;
141 allocator->gcalloc = stdalloc__calloc;
142 allocator->gstrdup = stdalloc__strdup;
143 allocator->gstrndup = stdalloc__strndup;
144 allocator->gsubstrdup = stdalloc__substrdup;
145 allocator->grealloc = stdalloc__realloc;
146 allocator->greallocarray = stdalloc__reallocarray;
147 allocator->gmallocarray = stdalloc__mallocarray;
148 allocator->gfree = stdalloc__free;
149 return 0;
150}