File: | build-scan/../src/basic/unit-name.c |
Warning: | line 310, column 32 Use of zero-allocated memory |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | ||||
2 | |||||
3 | #include <errno(*__errno_location ()).h> | ||||
4 | #include <stddef.h> | ||||
5 | #include <stdint.h> | ||||
6 | #include <stdlib.h> | ||||
7 | #include <string.h> | ||||
8 | |||||
9 | #include "alloc-util.h" | ||||
10 | #include "glob-util.h" | ||||
11 | #include "hexdecoct.h" | ||||
12 | #include "path-util.h" | ||||
13 | #include "special.h" | ||||
14 | #include "string-util.h" | ||||
15 | #include "strv.h" | ||||
16 | #include "unit-name.h" | ||||
17 | |||||
18 | /* Characters valid in a unit name. */ | ||||
19 | #define VALID_CHARS"0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\" \ | ||||
20 | DIGITS"0123456789" \ | ||||
21 | LETTERS"abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ | ||||
22 | ":-_.\\" | ||||
23 | |||||
24 | /* The same, but also permits the single @ character that may appear */ | ||||
25 | #define VALID_CHARS_WITH_AT"@" "0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\" \ | ||||
26 | "@" \ | ||||
27 | VALID_CHARS"0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\" | ||||
28 | |||||
29 | /* All chars valid in a unit name glob */ | ||||
30 | #define VALID_CHARS_GLOB"@" "0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\" "[]!-*?" \ | ||||
31 | VALID_CHARS_WITH_AT"@" "0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\" \ | ||||
32 | "[]!-*?" | ||||
33 | |||||
34 | bool_Bool unit_name_is_valid(const char *n, UnitNameFlags flags) { | ||||
35 | const char *e, *i, *at; | ||||
36 | |||||
37 | assert((flags & ~(UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) == 0)do { if ((__builtin_expect(!!(!((flags & ~(UNIT_NAME_PLAIN |UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) == 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("(flags & ~(UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) == 0" ), "../src/basic/unit-name.c", 37, __PRETTY_FUNCTION__); } while (0); | ||||
38 | |||||
39 | if (_unlikely_(flags == 0)(__builtin_expect(!!(flags == 0),0))) | ||||
40 | return false0; | ||||
41 | |||||
42 | if (isempty(n)) | ||||
43 | return false0; | ||||
44 | |||||
45 | if (strlen(n) >= UNIT_NAME_MAX256) | ||||
46 | return false0; | ||||
47 | |||||
48 | e = strrchr(n, '.'); | ||||
49 | if (!e || e == n) | ||||
50 | return false0; | ||||
51 | |||||
52 | if (unit_type_from_string(e + 1) < 0) | ||||
53 | return false0; | ||||
54 | |||||
55 | for (i = n, at = NULL((void*)0); i < e; i++) { | ||||
56 | |||||
57 | if (*i == '@' && !at) | ||||
58 | at = i; | ||||
59 | |||||
60 | if (!strchr("@" VALID_CHARS"0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\", *i)) | ||||
61 | return false0; | ||||
62 | } | ||||
63 | |||||
64 | if (at == n) | ||||
65 | return false0; | ||||
66 | |||||
67 | if (flags & UNIT_NAME_PLAIN) | ||||
68 | if (!at) | ||||
69 | return true1; | ||||
70 | |||||
71 | if (flags & UNIT_NAME_INSTANCE) | ||||
72 | if (at && e > at + 1) | ||||
73 | return true1; | ||||
74 | |||||
75 | if (flags & UNIT_NAME_TEMPLATE) | ||||
76 | if (at && e == at + 1) | ||||
77 | return true1; | ||||
78 | |||||
79 | return false0; | ||||
80 | } | ||||
81 | |||||
82 | bool_Bool unit_prefix_is_valid(const char *p) { | ||||
83 | |||||
84 | /* We don't allow additional @ in the prefix string */ | ||||
85 | |||||
86 | if (isempty(p)) | ||||
87 | return false0; | ||||
88 | |||||
89 | return in_charset(p, VALID_CHARS"0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\"); | ||||
90 | } | ||||
91 | |||||
92 | bool_Bool unit_instance_is_valid(const char *i) { | ||||
93 | |||||
94 | /* The max length depends on the length of the string, so we | ||||
95 | * don't really check this here. */ | ||||
96 | |||||
97 | if (isempty(i)) | ||||
98 | return false0; | ||||
99 | |||||
100 | /* We allow additional @ in the instance string, we do not | ||||
101 | * allow them in the prefix! */ | ||||
102 | |||||
103 | return in_charset(i, "@" VALID_CHARS"0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\"); | ||||
104 | } | ||||
105 | |||||
106 | bool_Bool unit_suffix_is_valid(const char *s) { | ||||
107 | if (isempty(s)) | ||||
108 | return false0; | ||||
109 | |||||
110 | if (s[0] != '.') | ||||
111 | return false0; | ||||
112 | |||||
113 | if (unit_type_from_string(s + 1) < 0) | ||||
114 | return false0; | ||||
115 | |||||
116 | return true1; | ||||
117 | } | ||||
118 | |||||
119 | int unit_name_to_prefix(const char *n, char **ret) { | ||||
120 | const char *p; | ||||
121 | char *s; | ||||
122 | |||||
123 | assert(n)do { if ((__builtin_expect(!!(!(n)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("n"), "../src/basic/unit-name.c", 123, __PRETTY_FUNCTION__ ); } while (0); | ||||
124 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 124, __PRETTY_FUNCTION__); } while (0); | ||||
125 | |||||
126 | if (!unit_name_is_valid(n, UNIT_NAME_ANY)) | ||||
127 | return -EINVAL22; | ||||
128 | |||||
129 | p = strchr(n, '@'); | ||||
130 | if (!p) | ||||
131 | p = strrchr(n, '.'); | ||||
132 | |||||
133 | assert_se(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("p"), "../src/basic/unit-name.c", 133, __PRETTY_FUNCTION__ ); } while (0); | ||||
134 | |||||
135 | s = strndup(n, p - n); | ||||
136 | if (!s) | ||||
137 | return -ENOMEM12; | ||||
138 | |||||
139 | *ret = s; | ||||
140 | return 0; | ||||
141 | } | ||||
142 | |||||
143 | int unit_name_to_instance(const char *n, char **instance) { | ||||
144 | const char *p, *d; | ||||
145 | char *i; | ||||
146 | |||||
147 | assert(n)do { if ((__builtin_expect(!!(!(n)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("n"), "../src/basic/unit-name.c", 147, __PRETTY_FUNCTION__ ); } while (0); | ||||
148 | assert(instance)do { if ((__builtin_expect(!!(!(instance)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("instance"), "../src/basic/unit-name.c", 148, __PRETTY_FUNCTION__); } while (0); | ||||
149 | |||||
150 | if (!unit_name_is_valid(n, UNIT_NAME_ANY)) | ||||
151 | return -EINVAL22; | ||||
152 | |||||
153 | /* Everything past the first @ and before the last . is the instance */ | ||||
154 | p = strchr(n, '@'); | ||||
155 | if (!p) { | ||||
156 | *instance = NULL((void*)0); | ||||
157 | return 0; | ||||
158 | } | ||||
159 | |||||
160 | p++; | ||||
161 | |||||
162 | d = strrchr(p, '.'); | ||||
163 | if (!d) | ||||
164 | return -EINVAL22; | ||||
165 | |||||
166 | i = strndup(p, d-p); | ||||
167 | if (!i) | ||||
168 | return -ENOMEM12; | ||||
169 | |||||
170 | *instance = i; | ||||
171 | return 1; | ||||
172 | } | ||||
173 | |||||
174 | int unit_name_to_prefix_and_instance(const char *n, char **ret) { | ||||
175 | const char *d; | ||||
176 | char *s; | ||||
177 | |||||
178 | assert(n)do { if ((__builtin_expect(!!(!(n)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("n"), "../src/basic/unit-name.c", 178, __PRETTY_FUNCTION__ ); } while (0); | ||||
179 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 179, __PRETTY_FUNCTION__); } while (0); | ||||
180 | |||||
181 | if (!unit_name_is_valid(n, UNIT_NAME_ANY)) | ||||
182 | return -EINVAL22; | ||||
183 | |||||
184 | d = strrchr(n, '.'); | ||||
185 | if (!d) | ||||
186 | return -EINVAL22; | ||||
187 | |||||
188 | s = strndup(n, d - n); | ||||
189 | if (!s) | ||||
190 | return -ENOMEM12; | ||||
191 | |||||
192 | *ret = s; | ||||
193 | return 0; | ||||
194 | } | ||||
195 | |||||
196 | UnitType unit_name_to_type(const char *n) { | ||||
197 | const char *e; | ||||
198 | |||||
199 | assert(n)do { if ((__builtin_expect(!!(!(n)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("n"), "../src/basic/unit-name.c", 199, __PRETTY_FUNCTION__ ); } while (0); | ||||
200 | |||||
201 | if (!unit_name_is_valid(n, UNIT_NAME_ANY)) | ||||
202 | return _UNIT_TYPE_INVALID; | ||||
203 | |||||
204 | assert_se(e = strrchr(n, '.'))do { if ((__builtin_expect(!!(!(e = strrchr(n, '.'))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("e = strrchr(n, '.')"), "../src/basic/unit-name.c" , 204, __PRETTY_FUNCTION__); } while (0); | ||||
205 | |||||
206 | return unit_type_from_string(e + 1); | ||||
207 | } | ||||
208 | |||||
209 | int unit_name_change_suffix(const char *n, const char *suffix, char **ret) { | ||||
210 | char *e, *s; | ||||
211 | size_t a, b; | ||||
212 | |||||
213 | assert(n)do { if ((__builtin_expect(!!(!(n)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("n"), "../src/basic/unit-name.c", 213, __PRETTY_FUNCTION__ ); } while (0); | ||||
214 | assert(suffix)do { if ((__builtin_expect(!!(!(suffix)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("suffix"), "../src/basic/unit-name.c", 214 , __PRETTY_FUNCTION__); } while (0); | ||||
215 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 215, __PRETTY_FUNCTION__); } while (0); | ||||
216 | |||||
217 | if (!unit_name_is_valid(n, UNIT_NAME_ANY)) | ||||
218 | return -EINVAL22; | ||||
219 | |||||
220 | if (!unit_suffix_is_valid(suffix)) | ||||
221 | return -EINVAL22; | ||||
222 | |||||
223 | assert_se(e = strrchr(n, '.'))do { if ((__builtin_expect(!!(!(e = strrchr(n, '.'))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("e = strrchr(n, '.')"), "../src/basic/unit-name.c" , 223, __PRETTY_FUNCTION__); } while (0); | ||||
224 | |||||
225 | a = e - n; | ||||
226 | b = strlen(suffix); | ||||
227 | |||||
228 | s = new(char, a + b + 1)((char*) malloc_multiply(sizeof(char), (a + b + 1))); | ||||
229 | if (!s) | ||||
230 | return -ENOMEM12; | ||||
231 | |||||
232 | strcpy(mempcpy(s, n, a), suffix); | ||||
233 | *ret = s; | ||||
234 | |||||
235 | return 0; | ||||
236 | } | ||||
237 | |||||
238 | int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret) { | ||||
239 | UnitType type; | ||||
240 | |||||
241 | assert(prefix)do { if ((__builtin_expect(!!(!(prefix)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("prefix"), "../src/basic/unit-name.c", 241 , __PRETTY_FUNCTION__); } while (0); | ||||
242 | assert(suffix)do { if ((__builtin_expect(!!(!(suffix)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("suffix"), "../src/basic/unit-name.c", 242 , __PRETTY_FUNCTION__); } while (0); | ||||
243 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 243, __PRETTY_FUNCTION__); } while (0); | ||||
244 | |||||
245 | if (suffix[0] != '.') | ||||
246 | return -EINVAL22; | ||||
247 | |||||
248 | type = unit_type_from_string(suffix + 1); | ||||
249 | if (type < 0) | ||||
250 | return -EINVAL22; | ||||
251 | |||||
252 | return unit_name_build_from_type(prefix, instance, type, ret); | ||||
253 | } | ||||
254 | |||||
255 | int unit_name_build_from_type(const char *prefix, const char *instance, UnitType type, char **ret) { | ||||
256 | const char *ut; | ||||
257 | char *s; | ||||
258 | |||||
259 | assert(prefix)do { if ((__builtin_expect(!!(!(prefix)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("prefix"), "../src/basic/unit-name.c", 259 , __PRETTY_FUNCTION__); } while (0); | ||||
260 | assert(type >= 0)do { if ((__builtin_expect(!!(!(type >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("type >= 0"), "../src/basic/unit-name.c" , 260, __PRETTY_FUNCTION__); } while (0); | ||||
261 | assert(type < _UNIT_TYPE_MAX)do { if ((__builtin_expect(!!(!(type < _UNIT_TYPE_MAX)),0) )) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("type < _UNIT_TYPE_MAX" ), "../src/basic/unit-name.c", 261, __PRETTY_FUNCTION__); } while (0); | ||||
262 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 262, __PRETTY_FUNCTION__); } while (0); | ||||
263 | |||||
264 | if (!unit_prefix_is_valid(prefix)) | ||||
265 | return -EINVAL22; | ||||
266 | |||||
267 | if (instance && !unit_instance_is_valid(instance)) | ||||
268 | return -EINVAL22; | ||||
269 | |||||
270 | ut = unit_type_to_string(type); | ||||
271 | |||||
272 | if (!instance) | ||||
273 | s = strjoin(prefix, ".", ut)strjoin_real((prefix), ".", ut, ((void*)0)); | ||||
274 | else | ||||
275 | s = strjoin(prefix, "@", instance, ".", ut)strjoin_real((prefix), "@", instance, ".", ut, ((void*)0)); | ||||
276 | if (!s) | ||||
277 | return -ENOMEM12; | ||||
278 | |||||
279 | *ret = s; | ||||
280 | return 0; | ||||
281 | } | ||||
282 | |||||
283 | static char *do_escape_char(char c, char *t) { | ||||
284 | assert(t)do { if ((__builtin_expect(!!(!(t)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("t"), "../src/basic/unit-name.c", 284, __PRETTY_FUNCTION__ ); } while (0); | ||||
285 | |||||
286 | *(t++) = '\\'; | ||||
287 | *(t++) = 'x'; | ||||
288 | *(t++) = hexchar(c >> 4); | ||||
289 | *(t++) = hexchar(c); | ||||
290 | |||||
291 | return t; | ||||
292 | } | ||||
293 | |||||
294 | static char *do_escape(const char *f, char *t) { | ||||
295 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/basic/unit-name.c", 295, __PRETTY_FUNCTION__ ); } while (0); | ||||
296 | assert(t)do { if ((__builtin_expect(!!(!(t)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("t"), "../src/basic/unit-name.c", 296, __PRETTY_FUNCTION__ ); } while (0); | ||||
297 | |||||
298 | /* do not create units with a leading '.', like for "/.dotdir" mount points */ | ||||
299 | if (*f == '.') { | ||||
300 | t = do_escape_char(*f, t); | ||||
301 | f++; | ||||
302 | } | ||||
303 | |||||
304 | for (; *f; f++) { | ||||
305 | if (*f == '/') | ||||
306 | *(t++) = '-'; | ||||
307 | else if (IN_SET(*f, '-', '\\')({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){'-', '\\'})/sizeof(int)]; switch(*f) { case '-': case '\\': _found = 1; break; default: break; } _found; }) || !strchr(VALID_CHARS"0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\", *f)) | ||||
308 | t = do_escape_char(*f, t); | ||||
309 | else | ||||
310 | *(t++) = *f; | ||||
| |||||
311 | } | ||||
312 | |||||
313 | return t; | ||||
314 | } | ||||
315 | |||||
316 | char *unit_name_escape(const char *f) { | ||||
317 | char *r, *t; | ||||
318 | |||||
319 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/basic/unit-name.c", 319, __PRETTY_FUNCTION__ ); } while (0); | ||||
320 | |||||
321 | r = new(char, strlen(f)*4+1)((char*) malloc_multiply(sizeof(char), (strlen(f)*4+1))); | ||||
322 | if (!r) | ||||
323 | return NULL((void*)0); | ||||
324 | |||||
325 | t = do_escape(f, r); | ||||
326 | *t = 0; | ||||
327 | |||||
328 | return r; | ||||
329 | } | ||||
330 | |||||
331 | int unit_name_unescape(const char *f, char **ret) { | ||||
332 | _cleanup_free___attribute__((cleanup(freep))) char *r = NULL((void*)0); | ||||
333 | char *t; | ||||
334 | |||||
335 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/basic/unit-name.c", 335, __PRETTY_FUNCTION__ ); } while (0); | ||||
336 | |||||
337 | r = strdup(f); | ||||
338 | if (!r) | ||||
339 | return -ENOMEM12; | ||||
340 | |||||
341 | for (t = r; *f; f++) { | ||||
342 | if (*f == '-') | ||||
343 | *(t++) = '/'; | ||||
344 | else if (*f == '\\') { | ||||
345 | int a, b; | ||||
346 | |||||
347 | if (f[1] != 'x') | ||||
348 | return -EINVAL22; | ||||
349 | |||||
350 | a = unhexchar(f[2]); | ||||
351 | if (a < 0) | ||||
352 | return -EINVAL22; | ||||
353 | |||||
354 | b = unhexchar(f[3]); | ||||
355 | if (b < 0) | ||||
356 | return -EINVAL22; | ||||
357 | |||||
358 | *(t++) = (char) (((uint8_t) a << 4U) | (uint8_t) b); | ||||
359 | f += 3; | ||||
360 | } else | ||||
361 | *(t++) = *f; | ||||
362 | } | ||||
363 | |||||
364 | *t = 0; | ||||
365 | |||||
366 | *ret = TAKE_PTR(r)({ typeof(r) _ptr_ = (r); (r) = ((void*)0); _ptr_; }); | ||||
367 | |||||
368 | return 0; | ||||
369 | } | ||||
370 | |||||
371 | int unit_name_path_escape(const char *f, char **ret) { | ||||
372 | char *p, *s; | ||||
373 | |||||
374 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/basic/unit-name.c", 374, __PRETTY_FUNCTION__ ); } while (0); | ||||
375 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 375, __PRETTY_FUNCTION__); } while (0); | ||||
376 | |||||
377 | p = strdupa(f)(__extension__ ({ const char *__old = (f); size_t __len = strlen (__old) + 1; char *__new = (char *) __builtin_alloca (__len) ; (char *) memcpy (__new, __old, __len); })); | ||||
378 | if (!p
| ||||
379 | return -ENOMEM12; | ||||
380 | |||||
381 | path_simplify(p, false0); | ||||
382 | |||||
383 | if (empty_or_root(p)) | ||||
384 | s = strdup("-"); | ||||
385 | else { | ||||
386 | if (!path_is_normalized(p)) | ||||
387 | return -EINVAL22; | ||||
388 | |||||
389 | /* Truncate trailing slashes */ | ||||
390 | delete_trailing_chars(p, "/"); | ||||
391 | |||||
392 | /* Truncate leading slashes */ | ||||
393 | p = skip_leading_chars(p, "/"); | ||||
394 | |||||
395 | s = unit_name_escape(p); | ||||
396 | } | ||||
397 | if (!s) | ||||
398 | return -ENOMEM12; | ||||
399 | |||||
400 | *ret = s; | ||||
401 | return 0; | ||||
402 | } | ||||
403 | |||||
404 | int unit_name_path_unescape(const char *f, char **ret) { | ||||
405 | char *s; | ||||
406 | int r; | ||||
407 | |||||
408 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/basic/unit-name.c", 408, __PRETTY_FUNCTION__ ); } while (0); | ||||
409 | |||||
410 | if (isempty(f)) | ||||
411 | return -EINVAL22; | ||||
412 | |||||
413 | if (streq(f, "-")(strcmp((f),("-")) == 0)) { | ||||
414 | s = strdup("/"); | ||||
415 | if (!s) | ||||
416 | return -ENOMEM12; | ||||
417 | } else { | ||||
418 | char *w; | ||||
419 | |||||
420 | r = unit_name_unescape(f, &w); | ||||
421 | if (r < 0) | ||||
422 | return r; | ||||
423 | |||||
424 | /* Don't accept trailing or leading slashes */ | ||||
425 | if (startswith(w, "/") || endswith(w, "/")) { | ||||
426 | free(w); | ||||
427 | return -EINVAL22; | ||||
428 | } | ||||
429 | |||||
430 | /* Prefix a slash again */ | ||||
431 | s = strappend("/", w); | ||||
432 | free(w); | ||||
433 | if (!s) | ||||
434 | return -ENOMEM12; | ||||
435 | |||||
436 | if (!path_is_normalized(s)) { | ||||
437 | free(s); | ||||
438 | return -EINVAL22; | ||||
439 | } | ||||
440 | } | ||||
441 | |||||
442 | if (ret) | ||||
443 | *ret = s; | ||||
444 | else | ||||
445 | free(s); | ||||
446 | |||||
447 | return 0; | ||||
448 | } | ||||
449 | |||||
450 | int unit_name_replace_instance(const char *f, const char *i, char **ret) { | ||||
451 | const char *p, *e; | ||||
452 | char *s; | ||||
453 | size_t a, b; | ||||
454 | |||||
455 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/basic/unit-name.c", 455, __PRETTY_FUNCTION__ ); } while (0); | ||||
456 | assert(i)do { if ((__builtin_expect(!!(!(i)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("i"), "../src/basic/unit-name.c", 456, __PRETTY_FUNCTION__ ); } while (0); | ||||
457 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 457, __PRETTY_FUNCTION__); } while (0); | ||||
458 | |||||
459 | if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) | ||||
460 | return -EINVAL22; | ||||
461 | if (!unit_instance_is_valid(i)) | ||||
462 | return -EINVAL22; | ||||
463 | |||||
464 | assert_se(p = strchr(f, '@'))do { if ((__builtin_expect(!!(!(p = strchr(f, '@'))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("p = strchr(f, '@')"), "../src/basic/unit-name.c" , 464, __PRETTY_FUNCTION__); } while (0); | ||||
465 | assert_se(e = strrchr(f, '.'))do { if ((__builtin_expect(!!(!(e = strrchr(f, '.'))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("e = strrchr(f, '.')"), "../src/basic/unit-name.c" , 465, __PRETTY_FUNCTION__); } while (0); | ||||
466 | |||||
467 | a = p - f; | ||||
468 | b = strlen(i); | ||||
469 | |||||
470 | s = new(char, a + 1 + b + strlen(e) + 1)((char*) malloc_multiply(sizeof(char), (a + 1 + b + strlen(e) + 1))); | ||||
471 | if (!s) | ||||
472 | return -ENOMEM12; | ||||
473 | |||||
474 | strcpy(mempcpy(mempcpy(s, f, a + 1), i, b), e); | ||||
475 | |||||
476 | *ret = s; | ||||
477 | return 0; | ||||
478 | } | ||||
479 | |||||
480 | int unit_name_template(const char *f, char **ret) { | ||||
481 | const char *p, *e; | ||||
482 | char *s; | ||||
483 | size_t a; | ||||
484 | |||||
485 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/basic/unit-name.c", 485, __PRETTY_FUNCTION__ ); } while (0); | ||||
486 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 486, __PRETTY_FUNCTION__); } while (0); | ||||
487 | |||||
488 | if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) | ||||
489 | return -EINVAL22; | ||||
490 | |||||
491 | assert_se(p = strchr(f, '@'))do { if ((__builtin_expect(!!(!(p = strchr(f, '@'))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("p = strchr(f, '@')"), "../src/basic/unit-name.c" , 491, __PRETTY_FUNCTION__); } while (0); | ||||
492 | assert_se(e = strrchr(f, '.'))do { if ((__builtin_expect(!!(!(e = strrchr(f, '.'))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("e = strrchr(f, '.')"), "../src/basic/unit-name.c" , 492, __PRETTY_FUNCTION__); } while (0); | ||||
493 | |||||
494 | a = p - f; | ||||
495 | |||||
496 | s = new(char, a + 1 + strlen(e) + 1)((char*) malloc_multiply(sizeof(char), (a + 1 + strlen(e) + 1 ))); | ||||
497 | if (!s) | ||||
498 | return -ENOMEM12; | ||||
499 | |||||
500 | strcpy(mempcpy(s, f, a + 1), e); | ||||
501 | |||||
502 | *ret = s; | ||||
503 | return 0; | ||||
504 | } | ||||
505 | |||||
506 | int unit_name_from_path(const char *path, const char *suffix, char **ret) { | ||||
507 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); | ||||
508 | char *s = NULL((void*)0); | ||||
509 | int r; | ||||
510 | |||||
511 | assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("path"), "../src/basic/unit-name.c", 511 , __PRETTY_FUNCTION__); } while (0); | ||||
512 | assert(suffix)do { if ((__builtin_expect(!!(!(suffix)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("suffix"), "../src/basic/unit-name.c", 512 , __PRETTY_FUNCTION__); } while (0); | ||||
513 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 513, __PRETTY_FUNCTION__); } while (0); | ||||
514 | |||||
515 | if (!unit_suffix_is_valid(suffix)) | ||||
516 | return -EINVAL22; | ||||
517 | |||||
518 | r = unit_name_path_escape(path, &p); | ||||
519 | if (r < 0) | ||||
520 | return r; | ||||
521 | |||||
522 | s = strappend(p, suffix); | ||||
523 | if (!s) | ||||
524 | return -ENOMEM12; | ||||
525 | |||||
526 | *ret = s; | ||||
527 | return 0; | ||||
528 | } | ||||
529 | |||||
530 | int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret) { | ||||
531 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); | ||||
532 | char *s; | ||||
533 | int r; | ||||
534 | |||||
535 | assert(prefix)do { if ((__builtin_expect(!!(!(prefix)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("prefix"), "../src/basic/unit-name.c", 535 , __PRETTY_FUNCTION__); } while (0); | ||||
| |||||
536 | assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("path"), "../src/basic/unit-name.c", 536 , __PRETTY_FUNCTION__); } while (0); | ||||
537 | assert(suffix)do { if ((__builtin_expect(!!(!(suffix)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("suffix"), "../src/basic/unit-name.c", 537 , __PRETTY_FUNCTION__); } while (0); | ||||
538 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 538, __PRETTY_FUNCTION__); } while (0); | ||||
539 | |||||
540 | if (!unit_prefix_is_valid(prefix)) | ||||
541 | return -EINVAL22; | ||||
542 | |||||
543 | if (!unit_suffix_is_valid(suffix)) | ||||
544 | return -EINVAL22; | ||||
545 | |||||
546 | r = unit_name_path_escape(path, &p); | ||||
547 | if (r < 0) | ||||
548 | return r; | ||||
549 | |||||
550 | s = strjoin(prefix, "@", p, suffix)strjoin_real((prefix), "@", p, suffix, ((void*)0)); | ||||
551 | if (!s) | ||||
552 | return -ENOMEM12; | ||||
553 | |||||
554 | *ret = s; | ||||
555 | return 0; | ||||
556 | } | ||||
557 | |||||
558 | int unit_name_to_path(const char *name, char **ret) { | ||||
559 | _cleanup_free___attribute__((cleanup(freep))) char *prefix = NULL((void*)0); | ||||
560 | int r; | ||||
561 | |||||
562 | assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name"), "../src/basic/unit-name.c", 562 , __PRETTY_FUNCTION__); } while (0); | ||||
563 | |||||
564 | r = unit_name_to_prefix(name, &prefix); | ||||
565 | if (r < 0) | ||||
566 | return r; | ||||
567 | |||||
568 | return unit_name_path_unescape(prefix, ret); | ||||
569 | } | ||||
570 | |||||
571 | static bool_Bool do_escape_mangle(const char *f, bool_Bool allow_globs, char *t) { | ||||
572 | const char *valid_chars; | ||||
573 | bool_Bool mangled = false0; | ||||
574 | |||||
575 | assert(f)do { if ((__builtin_expect(!!(!(f)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("f"), "../src/basic/unit-name.c", 575, __PRETTY_FUNCTION__ ); } while (0); | ||||
576 | assert(t)do { if ((__builtin_expect(!!(!(t)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("t"), "../src/basic/unit-name.c", 576, __PRETTY_FUNCTION__ ); } while (0); | ||||
577 | |||||
578 | /* We'll only escape the obvious characters here, to play safe. | ||||
579 | * | ||||
580 | * Returns true if any characters were mangled, false otherwise. | ||||
581 | */ | ||||
582 | |||||
583 | valid_chars = allow_globs ? VALID_CHARS_GLOB"@" "0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\" "[]!-*?" : VALID_CHARS_WITH_AT"@" "0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\"; | ||||
584 | |||||
585 | for (; *f; f++) | ||||
586 | if (*f == '/') { | ||||
587 | *(t++) = '-'; | ||||
588 | mangled = true1; | ||||
589 | } else if (!strchr(valid_chars, *f)) { | ||||
590 | t = do_escape_char(*f, t); | ||||
591 | mangled = true1; | ||||
592 | } else | ||||
593 | *(t++) = *f; | ||||
594 | *t = 0; | ||||
595 | |||||
596 | return mangled; | ||||
597 | } | ||||
598 | |||||
599 | /** | ||||
600 | * Convert a string to a unit name. /dev/blah is converted to dev-blah.device, | ||||
601 | * /blah/blah is converted to blah-blah.mount, anything else is left alone, | ||||
602 | * except that @suffix is appended if a valid unit suffix is not present. | ||||
603 | * | ||||
604 | * If @allow_globs, globs characters are preserved. Otherwise, they are escaped. | ||||
605 | */ | ||||
606 | int unit_name_mangle_with_suffix(const char *name, UnitNameMangle flags, const char *suffix, char **ret) { | ||||
607 | char *s; | ||||
608 | int r; | ||||
609 | bool_Bool mangled; | ||||
610 | |||||
611 | assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name"), "../src/basic/unit-name.c", 611 , __PRETTY_FUNCTION__); } while (0); | ||||
612 | assert(suffix)do { if ((__builtin_expect(!!(!(suffix)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("suffix"), "../src/basic/unit-name.c", 612 , __PRETTY_FUNCTION__); } while (0); | ||||
613 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 613, __PRETTY_FUNCTION__); } while (0); | ||||
614 | |||||
615 | if (isempty(name)) /* We cannot mangle empty unit names to become valid, sorry. */ | ||||
616 | return -EINVAL22; | ||||
617 | |||||
618 | if (!unit_suffix_is_valid(suffix)) | ||||
619 | return -EINVAL22; | ||||
620 | |||||
621 | /* Already a fully valid unit name? If so, no mangling is necessary... */ | ||||
622 | if (unit_name_is_valid(name, UNIT_NAME_ANY)) | ||||
623 | goto good; | ||||
624 | |||||
625 | /* Already a fully valid globbing expression? If so, no mangling is necessary either... */ | ||||
626 | if ((flags & UNIT_NAME_MANGLE_GLOB) && | ||||
627 | string_is_glob(name) && | ||||
628 | in_charset(name, VALID_CHARS_GLOB"@" "0123456789" "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ":-_.\\" "[]!-*?")) | ||||
629 | goto good; | ||||
630 | |||||
631 | if (is_device_path(name)) { | ||||
632 | r = unit_name_from_path(name, ".device", ret); | ||||
633 | if (r >= 0) | ||||
634 | return 1; | ||||
635 | if (r != -EINVAL22) | ||||
636 | return r; | ||||
637 | } | ||||
638 | |||||
639 | if (path_is_absolute(name)) { | ||||
640 | r = unit_name_from_path(name, ".mount", ret); | ||||
641 | if (r >= 0) | ||||
642 | return 1; | ||||
643 | if (r != -EINVAL22) | ||||
644 | return r; | ||||
645 | } | ||||
646 | |||||
647 | s = new(char, strlen(name) * 4 + strlen(suffix) + 1)((char*) malloc_multiply(sizeof(char), (strlen(name) * 4 + strlen (suffix) + 1))); | ||||
648 | if (!s) | ||||
649 | return -ENOMEM12; | ||||
650 | |||||
651 | mangled = do_escape_mangle(name, flags & UNIT_NAME_MANGLE_GLOB, s); | ||||
652 | if (mangled) | ||||
653 | log_full(flags & UNIT_NAME_MANGLE_WARN ? LOG_NOTICE : LOG_DEBUG,({ int _level = (((flags & UNIT_NAME_MANGLE_WARN ? 5 : 7) )), _e = ((0)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm (_realm) >= ((_level) & 0x07)) ? log_internal_realm((( _realm) << 10 | (_level)), _e, "../src/basic/unit-name.c" , 655, __func__, "Invalid unit name \"%s\" was escaped as \"%s\" (maybe you should use systemd-escape?)" , name, s) : -abs(_e); }) | ||||
654 | "Invalid unit name \"%s\" was escaped as \"%s\" (maybe you should use systemd-escape?)",({ int _level = (((flags & UNIT_NAME_MANGLE_WARN ? 5 : 7) )), _e = ((0)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm (_realm) >= ((_level) & 0x07)) ? log_internal_realm((( _realm) << 10 | (_level)), _e, "../src/basic/unit-name.c" , 655, __func__, "Invalid unit name \"%s\" was escaped as \"%s\" (maybe you should use systemd-escape?)" , name, s) : -abs(_e); }) | ||||
655 | name, s)({ int _level = (((flags & UNIT_NAME_MANGLE_WARN ? 5 : 7) )), _e = ((0)), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm (_realm) >= ((_level) & 0x07)) ? log_internal_realm((( _realm) << 10 | (_level)), _e, "../src/basic/unit-name.c" , 655, __func__, "Invalid unit name \"%s\" was escaped as \"%s\" (maybe you should use systemd-escape?)" , name, s) : -abs(_e); }); | ||||
656 | |||||
657 | /* Append a suffix if it doesn't have any, but only if this is not a glob, so that we can allow "foo.*" as a | ||||
658 | * valid glob. */ | ||||
659 | if ((!(flags & UNIT_NAME_MANGLE_GLOB) || !string_is_glob(s)) && unit_name_to_type(s) < 0) | ||||
660 | strcat(s, suffix); | ||||
661 | |||||
662 | *ret = s; | ||||
663 | return 1; | ||||
664 | |||||
665 | good: | ||||
666 | s = strdup(name); | ||||
667 | if (!s) | ||||
668 | return -ENOMEM12; | ||||
669 | |||||
670 | *ret = s; | ||||
671 | return 0; | ||||
672 | } | ||||
673 | |||||
674 | int slice_build_parent_slice(const char *slice, char **ret) { | ||||
675 | char *s, *dash; | ||||
676 | int r; | ||||
677 | |||||
678 | assert(slice)do { if ((__builtin_expect(!!(!(slice)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("slice"), "../src/basic/unit-name.c", 678 , __PRETTY_FUNCTION__); } while (0); | ||||
679 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 679, __PRETTY_FUNCTION__); } while (0); | ||||
680 | |||||
681 | if (!slice_name_is_valid(slice)) | ||||
682 | return -EINVAL22; | ||||
683 | |||||
684 | if (streq(slice, SPECIAL_ROOT_SLICE)(strcmp((slice),("-.slice")) == 0)) { | ||||
685 | *ret = NULL((void*)0); | ||||
686 | return 0; | ||||
687 | } | ||||
688 | |||||
689 | s = strdup(slice); | ||||
690 | if (!s) | ||||
691 | return -ENOMEM12; | ||||
692 | |||||
693 | dash = strrchr(s, '-'); | ||||
694 | if (dash) | ||||
695 | strcpy(dash, ".slice"); | ||||
696 | else { | ||||
697 | r = free_and_strdup(&s, SPECIAL_ROOT_SLICE"-.slice"); | ||||
698 | if (r < 0) { | ||||
699 | free(s); | ||||
700 | return r; | ||||
701 | } | ||||
702 | } | ||||
703 | |||||
704 | *ret = s; | ||||
705 | return 1; | ||||
706 | } | ||||
707 | |||||
708 | int slice_build_subslice(const char *slice, const char *name, char **ret) { | ||||
709 | char *subslice; | ||||
710 | |||||
711 | assert(slice)do { if ((__builtin_expect(!!(!(slice)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("slice"), "../src/basic/unit-name.c", 711 , __PRETTY_FUNCTION__); } while (0); | ||||
712 | assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name"), "../src/basic/unit-name.c", 712 , __PRETTY_FUNCTION__); } while (0); | ||||
713 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/unit-name.c", 713, __PRETTY_FUNCTION__); } while (0); | ||||
714 | |||||
715 | if (!slice_name_is_valid(slice)) | ||||
716 | return -EINVAL22; | ||||
717 | |||||
718 | if (!unit_prefix_is_valid(name)) | ||||
719 | return -EINVAL22; | ||||
720 | |||||
721 | if (streq(slice, SPECIAL_ROOT_SLICE)(strcmp((slice),("-.slice")) == 0)) | ||||
722 | subslice = strappend(name, ".slice"); | ||||
723 | else { | ||||
724 | char *e; | ||||
725 | |||||
726 | assert_se(e = endswith(slice, ".slice"))do { if ((__builtin_expect(!!(!(e = endswith(slice, ".slice") )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("e = endswith(slice, \".slice\")" ), "../src/basic/unit-name.c", 726, __PRETTY_FUNCTION__); } while (0); | ||||
727 | |||||
728 | subslice = new(char, (e - slice) + 1 + strlen(name) + 6 + 1)((char*) malloc_multiply(sizeof(char), ((e - slice) + 1 + strlen (name) + 6 + 1))); | ||||
729 | if (!subslice) | ||||
730 | return -ENOMEM12; | ||||
731 | |||||
732 | stpcpy(stpcpy(stpcpy(mempcpy(subslice, slice, e - slice), "-"), name), ".slice"); | ||||
733 | } | ||||
734 | |||||
735 | *ret = subslice; | ||||
736 | return 0; | ||||
737 | } | ||||
738 | |||||
739 | bool_Bool slice_name_is_valid(const char *name) { | ||||
740 | const char *p, *e; | ||||
741 | bool_Bool dash = false0; | ||||
742 | |||||
743 | if (!unit_name_is_valid(name, UNIT_NAME_PLAIN)) | ||||
744 | return false0; | ||||
745 | |||||
746 | if (streq(name, SPECIAL_ROOT_SLICE)(strcmp((name),("-.slice")) == 0)) | ||||
747 | return true1; | ||||
748 | |||||
749 | e = endswith(name, ".slice"); | ||||
750 | if (!e) | ||||
751 | return false0; | ||||
752 | |||||
753 | for (p = name; p < e; p++) { | ||||
754 | |||||
755 | if (*p == '-') { | ||||
756 | |||||
757 | /* Don't allow initial dash */ | ||||
758 | if (p == name) | ||||
759 | return false0; | ||||
760 | |||||
761 | /* Don't allow multiple dashes */ | ||||
762 | if (dash) | ||||
763 | return false0; | ||||
764 | |||||
765 | dash = true1; | ||||
766 | } else | ||||
767 | dash = false0; | ||||
768 | } | ||||
769 | |||||
770 | /* Don't allow trailing hash */ | ||||
771 | if (dash) | ||||
772 | return false0; | ||||
773 | |||||
774 | return true1; | ||||
775 | } |
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 | }) |