File: | build-scan/../src/core/unit-printf.c |
Warning: | line 306, column 12 Use of zero-allocated memory |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |||
2 | ||||
3 | #include "alloc-util.h" | |||
4 | #include "cgroup-util.h" | |||
5 | #include "format-util.h" | |||
6 | #include "macro.h" | |||
7 | #include "specifier.h" | |||
8 | #include "string-util.h" | |||
9 | #include "strv.h" | |||
10 | #include "unit-name.h" | |||
11 | #include "unit-printf.h" | |||
12 | #include "unit.h" | |||
13 | #include "user-util.h" | |||
14 | ||||
15 | static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) { | |||
16 | Unit *u = userdata; | |||
17 | ||||
18 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 18, __PRETTY_FUNCTION__ ); } while (0); | |||
19 | ||||
20 | return unit_name_to_prefix_and_instance(u->id, ret); | |||
21 | } | |||
22 | ||||
23 | static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) { | |||
24 | Unit *u = userdata; | |||
25 | ||||
26 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 26, __PRETTY_FUNCTION__ ); } while (0); | |||
27 | ||||
28 | return unit_name_to_prefix(u->id, ret); | |||
29 | } | |||
30 | ||||
31 | static int specifier_prefix_unescaped(char specifier, void *data, void *userdata, char **ret) { | |||
32 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); | |||
33 | Unit *u = userdata; | |||
34 | int r; | |||
35 | ||||
36 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 36, __PRETTY_FUNCTION__ ); } while (0); | |||
37 | ||||
38 | r = unit_name_to_prefix(u->id, &p); | |||
39 | if (r < 0) | |||
40 | return r; | |||
41 | ||||
42 | return unit_name_unescape(p, ret); | |||
43 | } | |||
44 | ||||
45 | static int specifier_instance_unescaped(char specifier, void *data, void *userdata, char **ret) { | |||
46 | Unit *u = userdata; | |||
47 | ||||
48 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 48, __PRETTY_FUNCTION__ ); } while (0); | |||
49 | ||||
50 | return unit_name_unescape(strempty(u->instance), ret); | |||
51 | } | |||
52 | ||||
53 | static int specifier_last_component(char specifier, void *data, void *userdata, char **ret) { | |||
54 | Unit *u = userdata; | |||
55 | _cleanup_free___attribute__((cleanup(freep))) char *prefix = NULL((void*)0); | |||
56 | char *dash; | |||
57 | int r; | |||
58 | ||||
59 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 59, __PRETTY_FUNCTION__ ); } while (0); | |||
60 | ||||
61 | r = unit_name_to_prefix(u->id, &prefix); | |||
62 | if (r < 0) | |||
63 | return r; | |||
64 | ||||
65 | dash = strrchr(prefix, '-'); | |||
66 | if (dash) | |||
67 | return specifier_string(specifier, dash + 1, userdata, ret); | |||
68 | ||||
69 | *ret = TAKE_PTR(prefix)({ typeof(prefix) _ptr_ = (prefix); (prefix) = ((void*)0); _ptr_ ; }); | |||
70 | return 0; | |||
71 | } | |||
72 | ||||
73 | static int specifier_last_component_unescaped(char specifier, void *data, void *userdata, char **ret) { | |||
74 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); | |||
75 | int r; | |||
76 | ||||
77 | r = specifier_last_component(specifier, data, userdata, &p); | |||
78 | if (r < 0) | |||
79 | return r; | |||
80 | ||||
81 | return unit_name_unescape(p, ret); | |||
82 | } | |||
83 | ||||
84 | static int specifier_filename(char specifier, void *data, void *userdata, char **ret) { | |||
85 | Unit *u = userdata; | |||
86 | ||||
87 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 87, __PRETTY_FUNCTION__ ); } while (0); | |||
88 | ||||
89 | if (u->instance) | |||
90 | return unit_name_path_unescape(u->instance, ret); | |||
91 | else | |||
92 | return unit_name_to_path(u->id, ret); | |||
93 | } | |||
94 | ||||
95 | static void bad_specifier(Unit *u, char specifier) { | |||
96 | log_unit_warning(u, "Specifier '%%%c' used in unit configuration, which is deprecated. Please update your unit file, as it does not work as intended.", specifier)({ const Unit *_u = (u); _u ? log_object_internal(4, 0, "../src/core/unit-printf.c" , 96, __func__, _u->manager->unit_log_field, _u->id, _u->manager->invocation_log_field, _u->invocation_id_string , "Specifier '%%%c' used in unit configuration, which is deprecated. Please update your unit file, as it does not work as intended." , specifier) : log_internal_realm(((LOG_REALM_SYSTEMD) << 10 | ((4))), 0, "../src/core/unit-printf.c", 96, __func__, "Specifier '%%%c' used in unit configuration, which is deprecated. Please update your unit file, as it does not work as intended." , specifier); }); | |||
97 | } | |||
98 | ||||
99 | static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) { | |||
100 | Unit *u = userdata; | |||
101 | char *n; | |||
102 | ||||
103 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 103, __PRETTY_FUNCTION__ ); } while (0); | |||
104 | ||||
105 | bad_specifier(u, specifier); | |||
106 | ||||
107 | if (u->cgroup_path) | |||
108 | n = strdup(u->cgroup_path); | |||
109 | else | |||
110 | n = unit_default_cgroup_path(u); | |||
111 | if (!n) | |||
112 | return -ENOMEM12; | |||
113 | ||||
114 | *ret = n; | |||
115 | return 0; | |||
116 | } | |||
117 | ||||
118 | static int specifier_cgroup_root(char specifier, void *data, void *userdata, char **ret) { | |||
119 | Unit *u = userdata; | |||
120 | char *n; | |||
121 | ||||
122 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 122, __PRETTY_FUNCTION__ ); } while (0); | |||
123 | ||||
124 | bad_specifier(u, specifier); | |||
125 | ||||
126 | n = strdup(u->manager->cgroup_root); | |||
127 | if (!n) | |||
128 | return -ENOMEM12; | |||
129 | ||||
130 | *ret = n; | |||
131 | return 0; | |||
132 | } | |||
133 | ||||
134 | static int specifier_cgroup_slice(char specifier, void *data, void *userdata, char **ret) { | |||
135 | Unit *u = userdata; | |||
136 | char *n; | |||
137 | ||||
138 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 138, __PRETTY_FUNCTION__ ); } while (0); | |||
139 | ||||
140 | bad_specifier(u, specifier); | |||
141 | ||||
142 | if (UNIT_ISSET(u->slice)(!!(u->slice).target)) { | |||
143 | Unit *slice; | |||
144 | ||||
145 | slice = UNIT_DEREF(u->slice)((u->slice).target); | |||
146 | ||||
147 | if (slice->cgroup_path) | |||
148 | n = strdup(slice->cgroup_path); | |||
149 | else | |||
150 | n = unit_default_cgroup_path(slice); | |||
151 | } else | |||
152 | n = strdup(u->manager->cgroup_root); | |||
153 | if (!n) | |||
154 | return -ENOMEM12; | |||
155 | ||||
156 | *ret = n; | |||
157 | return 0; | |||
158 | } | |||
159 | ||||
160 | static int specifier_special_directory(char specifier, void *data, void *userdata, char **ret) { | |||
161 | Unit *u = userdata; | |||
162 | char *n = NULL((void*)0); | |||
163 | ||||
164 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 164, __PRETTY_FUNCTION__ ); } while (0); | |||
165 | ||||
166 | n = strdup(u->manager->prefix[PTR_TO_UINT(data)((unsigned int) ((uintptr_t) (data)))]); | |||
167 | if (!n) | |||
168 | return -ENOMEM12; | |||
169 | ||||
170 | *ret = n; | |||
171 | return 0; | |||
172 | } | |||
173 | ||||
174 | int unit_name_printf(Unit *u, const char* format, char **ret) { | |||
175 | ||||
176 | /* | |||
177 | * This will use the passed string as format string and replace the following specifiers (which should all be | |||
178 | * safe for inclusion in unit names): | |||
179 | * | |||
180 | * %n: the full id of the unit (foo@bar.waldo) | |||
181 | * %N: the id of the unit without the suffix (foo@bar) | |||
182 | * %p: the prefix (foo) | |||
183 | * %i: the instance (bar) | |||
184 | * | |||
185 | * %U: the UID of the running user | |||
186 | * %u: the username of the running user | |||
187 | * | |||
188 | * %m: the machine ID of the running system | |||
189 | * %H: the host name of the running system | |||
190 | * %b: the boot ID of the running system | |||
191 | */ | |||
192 | ||||
193 | const Specifier table[] = { | |||
194 | { 'n', specifier_string, u->id }, | |||
195 | { 'N', specifier_prefix_and_instance, NULL((void*)0) }, | |||
196 | { 'p', specifier_prefix, NULL((void*)0) }, | |||
197 | { 'i', specifier_string, u->instance }, | |||
198 | ||||
199 | { 'U', specifier_user_id, NULL((void*)0) }, | |||
200 | { 'u', specifier_user_name, NULL((void*)0) }, | |||
201 | ||||
202 | { 'm', specifier_machine_id, NULL((void*)0) }, | |||
203 | { 'H', specifier_host_name, NULL((void*)0) }, | |||
204 | { 'b', specifier_boot_id, NULL((void*)0) }, | |||
205 | {} | |||
206 | }; | |||
207 | ||||
208 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 208, __PRETTY_FUNCTION__ ); } while (0); | |||
209 | assert(format)do { if ((__builtin_expect(!!(!(format)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("format"), "../src/core/unit-printf.c", 209 , __PRETTY_FUNCTION__); } while (0); | |||
210 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/core/unit-printf.c", 210 , __PRETTY_FUNCTION__); } while (0); | |||
211 | ||||
212 | return specifier_printf(format, table, u, ret); | |||
213 | } | |||
214 | ||||
215 | int unit_full_printf(Unit *u, const char *format, char **ret) { | |||
216 | ||||
217 | /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of additional codes | |||
218 | * (which are likely not suitable for unescaped inclusion in unit names): | |||
219 | * | |||
220 | * %f: the unescaped instance if set, otherwise the id unescaped as path | |||
221 | * | |||
222 | * %c: cgroup path of unit (deprecated) | |||
223 | * %r: where units in this slice are placed in the cgroup tree (deprecated) | |||
224 | * %R: the root of this systemd's instance tree (deprecated) | |||
225 | * | |||
226 | * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR) | |||
227 | * %S: the state directory root (e.g. /var/lib or $XDG_CONFIG_HOME) | |||
228 | * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME) | |||
229 | * %L: the log directory root (e.g. /var/log or $XDG_CONFIG_HOME/log) | |||
230 | * %E: the configuration directory root (e.g. /etc or $XDG_CONFIG_HOME) | |||
231 | * %T: the temporary directory (e.g. /tmp, or $TMPDIR, $TEMP, $TMP) | |||
232 | * %V: the temporary directory for large, persistent stuff (e.g. /var/tmp, or $TMPDIR, $TEMP, $TMP) | |||
233 | * | |||
234 | * %h: the homedir of the running user | |||
235 | * %s: the shell of the running user | |||
236 | * | |||
237 | * %v: `uname -r` of the running system | |||
238 | * | |||
239 | * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of the unit | |||
240 | * file itself are broken by design, as they would resolve differently depending on whether they are used | |||
241 | * before or after the relevant configuration setting. Hence: don't add them. | |||
242 | */ | |||
243 | ||||
244 | const Specifier table[] = { | |||
245 | { 'n', specifier_string, u->id }, | |||
246 | { 'N', specifier_prefix_and_instance, NULL((void*)0) }, | |||
247 | { 'p', specifier_prefix, NULL((void*)0) }, | |||
248 | { 'P', specifier_prefix_unescaped, NULL((void*)0) }, | |||
249 | { 'i', specifier_string, u->instance }, | |||
250 | { 'I', specifier_instance_unescaped, NULL((void*)0) }, | |||
251 | { 'j', specifier_last_component, NULL((void*)0) }, | |||
252 | { 'J', specifier_last_component_unescaped, NULL((void*)0) }, | |||
253 | ||||
254 | { 'f', specifier_filename, NULL((void*)0) }, | |||
255 | { 'c', specifier_cgroup, NULL((void*)0) }, | |||
256 | { 'r', specifier_cgroup_slice, NULL((void*)0) }, | |||
257 | { 'R', specifier_cgroup_root, NULL((void*)0) }, | |||
258 | ||||
259 | { 't', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME)((void *) ((uintptr_t) (EXEC_DIRECTORY_RUNTIME))) }, | |||
260 | { 'S', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_STATE)((void *) ((uintptr_t) (EXEC_DIRECTORY_STATE))) }, | |||
261 | { 'C', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CACHE)((void *) ((uintptr_t) (EXEC_DIRECTORY_CACHE))) }, | |||
262 | { 'L', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_LOGS)((void *) ((uintptr_t) (EXEC_DIRECTORY_LOGS))) }, | |||
263 | { 'E', specifier_special_directory, UINT_TO_PTR(EXEC_DIRECTORY_CONFIGURATION)((void *) ((uintptr_t) (EXEC_DIRECTORY_CONFIGURATION))) }, | |||
264 | { 'T', specifier_tmp_dir, NULL((void*)0) }, | |||
265 | { 'V', specifier_var_tmp_dir, NULL((void*)0) }, | |||
266 | ||||
267 | { 'U', specifier_user_id, NULL((void*)0) }, | |||
268 | { 'u', specifier_user_name, NULL((void*)0) }, | |||
269 | { 'h', specifier_user_home, NULL((void*)0) }, | |||
270 | { 's', specifier_user_shell, NULL((void*)0) }, | |||
271 | ||||
272 | { 'm', specifier_machine_id, NULL((void*)0) }, | |||
273 | { 'H', specifier_host_name, NULL((void*)0) }, | |||
274 | { 'b', specifier_boot_id, NULL((void*)0) }, | |||
275 | { 'v', specifier_kernel_release, NULL((void*)0) }, | |||
276 | {} | |||
277 | }; | |||
278 | ||||
279 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 279, __PRETTY_FUNCTION__ ); } while (0); | |||
280 | assert(format)do { if ((__builtin_expect(!!(!(format)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("format"), "../src/core/unit-printf.c", 280 , __PRETTY_FUNCTION__); } while (0); | |||
281 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/core/unit-printf.c", 281 , __PRETTY_FUNCTION__); } while (0); | |||
282 | ||||
283 | return specifier_printf(format, table, u, ret); | |||
284 | } | |||
285 | ||||
286 | int unit_full_printf_strv(Unit *u, char **l, char ***ret) { | |||
287 | size_t n; | |||
288 | char **r, **i, **j; | |||
289 | int q; | |||
290 | ||||
291 | /* Applies unit_full_printf to every entry in l */ | |||
292 | ||||
293 | assert(u)do { if ((__builtin_expect(!!(!(u)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("u"), "../src/core/unit-printf.c", 293, __PRETTY_FUNCTION__ ); } while (0); | |||
| ||||
294 | ||||
295 | n = strv_length(l); | |||
296 | r = new(char*, n+1)((char**) malloc_multiply(sizeof(char*), (n+1))); | |||
297 | if (!r) | |||
298 | return -ENOMEM12; | |||
299 | ||||
300 | for (i = l, j = r; *i; i++, j++) { | |||
301 | q = unit_full_printf(u, *i, j); | |||
302 | if (q < 0) | |||
303 | goto fail; | |||
304 | } | |||
305 | ||||
306 | *j = NULL((void*)0); | |||
| ||||
307 | *ret = r; | |||
308 | return 0; | |||
309 | ||||
310 | fail: | |||
311 | for (j--; j >= r; j--) | |||
312 | free(*j); | |||
313 | ||||
314 | free(r); | |||
315 | return q; | |||
316 | } |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | #pragma once |
3 | |
4 | #include <alloca.h> |
5 | #include <stddef.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | |
9 | #include "macro.h" |
10 | |
11 | #define new(t, n)((t*) malloc_multiply(sizeof(t), (n))) ((t*) malloc_multiply(sizeof(t), (n))) |
12 | |
13 | #define new0(t, n)((t*) calloc((n), sizeof(t))) ((t*) calloc((n), sizeof(t))) |
14 | |
15 | #define newa(t, n)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)" ), "../src/basic/alloc-util.h", 15, __PRETTY_FUNCTION__); } while (0); (t*) __builtin_alloca (sizeof(t)*(n)); }) \ |
16 | ({ \ |
17 | assert(!size_multiply_overflow(sizeof(t), n))do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)" ), "../src/basic/alloc-util.h", 17, __PRETTY_FUNCTION__); } while (0); \ |
18 | (t*) alloca(sizeof(t)*(n))__builtin_alloca (sizeof(t)*(n)); \ |
19 | }) |
20 | |
21 | #define newa0(t, n)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)" ), "../src/basic/alloc-util.h", 21, __PRETTY_FUNCTION__); } while (0); (t*) ({ char *_new_; size_t _len_ = sizeof(t)*(n); _new_ = __builtin_alloca (_len_); (void *) memset(_new_, 0, _len_) ; }); }) \ |
22 | ({ \ |
23 | assert(!size_multiply_overflow(sizeof(t), n))do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)" ), "../src/basic/alloc-util.h", 23, __PRETTY_FUNCTION__); } while (0); \ |
24 | (t*) alloca0(sizeof(t)*(n))({ char *_new_; size_t _len_ = sizeof(t)*(n); _new_ = __builtin_alloca (_len_); (void *) memset(_new_, 0, _len_); }); \ |
25 | }) |
26 | |
27 | #define newdup(t, p, n)((t*) memdup_multiply(p, sizeof(t), (n))) ((t*) memdup_multiply(p, sizeof(t), (n))) |
28 | |
29 | #define newdup_suffix0(t, p, n)((t*) memdup_suffix0_multiply(p, sizeof(t), (n))) ((t*) memdup_suffix0_multiply(p, sizeof(t), (n))) |
30 | |
31 | #define malloc0(n)(calloc(1, (n))) (calloc(1, (n))) |
32 | |
33 | static inline void *mfree(void *memory) { |
34 | free(memory); |
35 | return NULL((void*)0); |
36 | } |
37 | |
38 | #define free_and_replace(a, b)({ free(a); (a) = (b); (b) = ((void*)0); 0; }) \ |
39 | ({ \ |
40 | free(a); \ |
41 | (a) = (b); \ |
42 | (b) = NULL((void*)0); \ |
43 | 0; \ |
44 | }) |
45 | |
46 | void* memdup(const void *p, size_t l) _alloc_(2); |
47 | void* memdup_suffix0(const void *p, size_t l) _alloc_(2); |
48 | |
49 | static inline void freep(void *p) { |
50 | free(*(void**) p); |
51 | } |
52 | |
53 | #define _cleanup_free___attribute__((cleanup(freep))) _cleanup_(freep)__attribute__((cleanup(freep))) |
54 | |
55 | static inline bool_Bool size_multiply_overflow(size_t size, size_t need) { |
56 | return _unlikely_(need != 0 && size > (SIZE_MAX / need))(__builtin_expect(!!(need != 0 && size > ((18446744073709551615UL ) / need)),0)); |
57 | } |
58 | |
59 | _malloc___attribute__ ((malloc)) _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) { |
60 | if (size_multiply_overflow(size, need)) |
61 | return NULL((void*)0); |
62 | |
63 | return malloc(size * need); |
64 | } |
65 | |
66 | #if !HAVE_REALLOCARRAY1 |
67 | _alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size) { |
68 | if (size_multiply_overflow(size, need)) |
69 | return NULL((void*)0); |
70 | |
71 | return realloc(p, size * need); |
72 | } |
73 | #endif |
74 | |
75 | _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) { |
76 | if (size_multiply_overflow(size, need)) |
77 | return NULL((void*)0); |
78 | |
79 | return memdup(p, size * need); |
80 | } |
81 | |
82 | _alloc_(2, 3) static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) { |
83 | if (size_multiply_overflow(size, need)) |
84 | return NULL((void*)0); |
85 | |
86 | return memdup_suffix0(p, size * need); |
87 | } |
88 | |
89 | void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size); |
90 | void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size); |
91 | |
92 | #define GREEDY_REALLOC(array, allocated, need)greedy_realloc((void**) &(array), &(allocated), (need ), sizeof((array)[0])) \ |
93 | greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0])) |
94 | |
95 | #define GREEDY_REALLOC0(array, allocated, need)greedy_realloc0((void**) &(array), &(allocated), (need ), sizeof((array)[0])) \ |
96 | greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0])) |
97 | |
98 | #define alloca0(n)({ char *_new_; size_t _len_ = n; _new_ = __builtin_alloca (_len_ ); (void *) memset(_new_, 0, _len_); }) \ |
99 | ({ \ |
100 | char *_new_; \ |
101 | size_t _len_ = n; \ |
102 | _new_ = alloca(_len_)__builtin_alloca (_len_); \ |
103 | (void *) memset(_new_, 0, _len_); \ |
104 | }) |
105 | |
106 | /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */ |
107 | #define alloca_align(size, align)({ void *_ptr_; size_t _mask_ = (align) - 1; _ptr_ = __builtin_alloca ((size) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); }) \ |
108 | ({ \ |
109 | void *_ptr_; \ |
110 | size_t _mask_ = (align) - 1; \ |
111 | _ptr_ = alloca((size) + _mask_)__builtin_alloca ((size) + _mask_); \ |
112 | (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \ |
113 | }) |
114 | |
115 | #define alloca0_align(size, align)({ void *_new_; size_t _size_ = (size); _new_ = ({ void *_ptr_ ; size_t _mask_ = ((align)) - 1; _ptr_ = __builtin_alloca ((_size_ ) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_ ); }); (void*)memset(_new_, 0, _size_); }) \ |
116 | ({ \ |
117 | void *_new_; \ |
118 | size_t _size_ = (size); \ |
119 | _new_ = alloca_align(_size_, (align))({ void *_ptr_; size_t _mask_ = ((align)) - 1; _ptr_ = __builtin_alloca ((_size_) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); }); \ |
120 | (void*)memset(_new_, 0, _size_); \ |
121 | }) |
122 | |
123 | /* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to |
124 | * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */ |
125 | #define TAKE_PTR(ptr)({ typeof(ptr) _ptr_ = (ptr); (ptr) = ((void*)0); _ptr_; }) \ |
126 | ({ \ |
127 | typeof(ptr) _ptr_ = (ptr); \ |
128 | (ptr) = NULL((void*)0); \ |
129 | _ptr_; \ |
130 | }) |