Coverage Report

Created: 2023-12-08 06:38

/src/casync/src/util.h
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: LGPL-2.1+ */
2
3
#ifndef fooutilhfoo
4
#define fooutilhfoo
5
6
#include <assert.h>
7
#include <dirent.h>
8
#include <errno.h>
9
#include <inttypes.h>
10
#include <signal.h>
11
#include <stdbool.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <string.h>
15
#include <sys/types.h>
16
#include <sys/vfs.h>
17
#include <unistd.h>
18
19
#include <linux/btrfs.h>
20
#include <linux/fs.h>
21
#include <linux/magic.h>
22
#include <linux/types.h>
23
24
#include "gcc-macro.h"
25
#include "log.h"
26
27
0
#define new(t, n) ((t*) malloc((n) * sizeof(t)))
28
#define new0(t, n) ((t*) calloc((n), sizeof(t)))
29
30
#define newa(t, n) ((t*) alloca((n) * sizeof(t)))
31
32
138
#define XCONCATENATE(x, y) x ## y
33
552
#define CONCATENATE(x, y) XCONCATENATE(x, y)
34
35
552
#define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
36
#define UNIQ __COUNTER__
37
38
#undef MAX
39
138
#define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
40
#define __MAX(aq, a, bq, b)                             \
41
138
        __extension__ ({                                \
42
138
                const typeof(a) UNIQ_T(A, aq) = (a);    \
43
138
                const typeof(b) UNIQ_T(B, bq) = (b);    \
44
138
                UNIQ_T(A,aq) > UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
45
138
        })
46
47
0
#define MAX3(a, b, c) MAX(MAX(a, b), c)
48
49
#undef MIN
50
0
#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
51
#define __MIN(aq, a, bq, b)                             \
52
0
        __extension__ ({                                \
53
0
                const typeof(a) UNIQ_T(A, aq) = (a);    \
54
0
                const typeof(b) UNIQ_T(B, bq) = (b);    \
55
0
                UNIQ_T(A,aq) < UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
56
0
        })
57
58
#define CONST_MAX(_A, _B) \
59
        __extension__ (__builtin_choose_expr(                           \
60
                __builtin_constant_p(_A) &&                             \
61
                __builtin_constant_p(_B) &&                             \
62
                __builtin_types_compatible_p(typeof(_A), typeof(_B)),   \
63
                ((_A) > (_B)) ? (_A) : (_B),                            \
64
                (void)0))
65
66
67
68
int loop_write(int fd, const void *p, size_t l);
69
int loop_write_block(int fd, const void *p, size_t l);
70
ssize_t loop_read(int fd, void *p, size_t l);
71
72
int write_zeroes(int fd, size_t l);
73
int loop_write_with_holes(int fd, const void *p, size_t l, uint64_t *ret_punched);
74
75
int skip_bytes(int fd, uint64_t bytes);
76
77
char *endswith(const char *p, const char *suffix);
78
79
0
static inline void* mfree(void* p) {
80
0
        free(p);
81
0
        return NULL;
82
0
}
Unexecuted instantiation: fuzz-compress.c:mfree
Unexecuted instantiation: compressor.c:mfree
Unexecuted instantiation: log.c:mfree
Unexecuted instantiation: util.c:mfree
83
84
0
static inline int safe_close_above(int above, int fd) {
85
0
        if (fd >= above) {
86
0
                int saved_errno = errno;
87
0
                assert_se(close(fd) >= 0 || errno != EBADF);
88
0
                errno = saved_errno;
89
0
        }
90
91
0
        return -1;
92
0
}
Unexecuted instantiation: fuzz-compress.c:safe_close_above
Unexecuted instantiation: compressor.c:safe_close_above
Unexecuted instantiation: log.c:safe_close_above
Unexecuted instantiation: util.c:safe_close_above
93
94
0
static inline int safe_close(int fd) {
95
0
        return safe_close_above(0, fd);
96
0
}
Unexecuted instantiation: fuzz-compress.c:safe_close
Unexecuted instantiation: compressor.c:safe_close
Unexecuted instantiation: log.c:safe_close
Unexecuted instantiation: util.c:safe_close
97
98
0
static inline int safe_closep(int *fd) {
99
0
        return safe_close(*fd);
100
0
}
Unexecuted instantiation: fuzz-compress.c:safe_closep
Unexecuted instantiation: compressor.c:safe_closep
Unexecuted instantiation: log.c:safe_closep
Unexecuted instantiation: util.c:safe_closep
101
102
0
static inline void safe_close_nonstdp(int *fd) {
103
0
        safe_close_above(STDERR_FILENO, *fd);
104
0
}
Unexecuted instantiation: fuzz-compress.c:safe_close_nonstdp
Unexecuted instantiation: compressor.c:safe_close_nonstdp
Unexecuted instantiation: log.c:safe_close_nonstdp
Unexecuted instantiation: util.c:safe_close_nonstdp
105
106
0
static inline FILE *safe_fclose(FILE *f) {
107
0
        if (f)
108
0
                fclose(f);
109
0
110
0
        return NULL;
111
0
}
Unexecuted instantiation: fuzz-compress.c:safe_fclose
Unexecuted instantiation: compressor.c:safe_fclose
Unexecuted instantiation: log.c:safe_fclose
Unexecuted instantiation: util.c:safe_fclose
112
113
0
static inline void safe_fclosep(FILE **f) {
114
0
        if (f && *f)
115
0
                fclose(*f);
116
0
}
Unexecuted instantiation: fuzz-compress.c:safe_fclosep
Unexecuted instantiation: compressor.c:safe_fclosep
Unexecuted instantiation: log.c:safe_fclosep
Unexecuted instantiation: util.c:safe_fclosep
117
118
typedef uint16_t le16_t;
119
typedef uint32_t le32_t;
120
typedef uint64_t le64_t;
121
122
0
static inline uint64_t read_le64(const void *p) {
123
0
        uint64_t u;
124
0
        assert(p);
125
0
        memcpy(&u, p, sizeof(uint64_t));
126
0
        return le64toh(u);
127
0
}
Unexecuted instantiation: fuzz-compress.c:read_le64
Unexecuted instantiation: compressor.c:read_le64
Unexecuted instantiation: log.c:read_le64
Unexecuted instantiation: util.c:read_le64
128
129
0
static inline uint32_t read_le32(const void *p) {
130
0
        uint32_t u;
131
0
        assert(p);
132
0
        memcpy(&u, p, sizeof(uint32_t));
133
0
        return le32toh(u);
134
0
}
Unexecuted instantiation: fuzz-compress.c:read_le32
Unexecuted instantiation: compressor.c:read_le32
Unexecuted instantiation: log.c:read_le32
Unexecuted instantiation: util.c:read_le32
135
136
0
static inline uint16_t read_le16(const void *p) {
137
0
        uint16_t u;
138
0
        assert(p);
139
0
        memcpy(&u, p, sizeof(uint16_t));
140
0
        return le16toh(u);
141
0
}
Unexecuted instantiation: fuzz-compress.c:read_le16
Unexecuted instantiation: compressor.c:read_le16
Unexecuted instantiation: log.c:read_le16
Unexecuted instantiation: util.c:read_le16
142
143
0
static inline void write_le64(void *p, uint64_t u) {
144
0
        assert(p);
145
0
        u = htole64(u);
146
0
        memcpy(p, &u, sizeof(uint64_t));
147
0
}
Unexecuted instantiation: fuzz-compress.c:write_le64
Unexecuted instantiation: compressor.c:write_le64
Unexecuted instantiation: log.c:write_le64
Unexecuted instantiation: util.c:write_le64
148
149
0
static inline void write_le32(void *p, uint32_t u) {
150
0
        assert(p);
151
0
        u = htole32(u);
152
0
        memcpy(p, &u, sizeof(uint32_t));
153
0
}
Unexecuted instantiation: fuzz-compress.c:write_le32
Unexecuted instantiation: compressor.c:write_le32
Unexecuted instantiation: log.c:write_le32
Unexecuted instantiation: util.c:write_le32
154
155
0
static inline void write_le16(void *p, uint16_t u) {
156
0
        assert(p);
157
0
        u = htole16(u);
158
0
        memcpy(p, &u, sizeof(uint16_t));
159
0
}
Unexecuted instantiation: fuzz-compress.c:write_le16
Unexecuted instantiation: compressor.c:write_le16
Unexecuted instantiation: log.c:write_le16
Unexecuted instantiation: util.c:write_le16
160
161
0
static inline void* memdup(const void *p, size_t size) {
162
0
        void *q;
163
0
164
0
        q = malloc(size);
165
0
        if (!q)
166
0
                return NULL;
167
0
168
0
        memcpy(q, p, size);
169
0
        return q;
170
0
}
Unexecuted instantiation: fuzz-compress.c:memdup
Unexecuted instantiation: compressor.c:memdup
Unexecuted instantiation: log.c:memdup
Unexecuted instantiation: util.c:memdup
171
172
int dev_urandom(void *p, size_t n);
173
174
0
static inline uint64_t random_u64(void) {
175
0
        uint64_t u;
176
0
        dev_urandom(&u, sizeof(u));
177
0
        return u;
178
0
}
Unexecuted instantiation: fuzz-compress.c:random_u64
Unexecuted instantiation: compressor.c:random_u64
Unexecuted instantiation: log.c:random_u64
Unexecuted instantiation: util.c:random_u64
179
180
#define random_bytes(p, n) dev_urandom(p, n)
181
182
#define assert_cc(expr) static_assert(expr, #expr)
183
184
0
#define CASE_F(X) case X:
185
0
#define CASE_F_1(CASE, X) CASE_F(X)
186
0
#define CASE_F_2(CASE, X, ...)  CASE(X) CASE_F_1(CASE, __VA_ARGS__)
187
0
#define CASE_F_3(CASE, X, ...)  CASE(X) CASE_F_2(CASE, __VA_ARGS__)
188
0
#define CASE_F_4(CASE, X, ...)  CASE(X) CASE_F_3(CASE, __VA_ARGS__)
189
0
#define CASE_F_5(CASE, X, ...)  CASE(X) CASE_F_4(CASE, __VA_ARGS__)
190
#define CASE_F_6(CASE, X, ...)  CASE(X) CASE_F_5(CASE, __VA_ARGS__)
191
#define CASE_F_7(CASE, X, ...)  CASE(X) CASE_F_6(CASE, __VA_ARGS__)
192
#define CASE_F_8(CASE, X, ...)  CASE(X) CASE_F_7(CASE, __VA_ARGS__)
193
#define CASE_F_9(CASE, X, ...)  CASE(X) CASE_F_8(CASE, __VA_ARGS__)
194
#define CASE_F_10(CASE, X, ...) CASE(X) CASE_F_9(CASE, __VA_ARGS__)
195
#define CASE_F_11(CASE, X, ...) CASE(X) CASE_F_10(CASE, __VA_ARGS__)
196
#define CASE_F_12(CASE, X, ...) CASE(X) CASE_F_11(CASE, __VA_ARGS__)
197
#define CASE_F_13(CASE, X, ...) CASE(X) CASE_F_12(CASE, __VA_ARGS__)
198
#define CASE_F_14(CASE, X, ...) CASE(X) CASE_F_13(CASE, __VA_ARGS__)
199
#define CASE_F_15(CASE, X, ...) CASE(X) CASE_F_14(CASE, __VA_ARGS__)
200
#define CASE_F_16(CASE, X, ...) CASE(X) CASE_F_15(CASE, __VA_ARGS__)
201
#define CASE_F_17(CASE, X, ...) CASE(X) CASE_F_16(CASE, __VA_ARGS__)
202
#define CASE_F_18(CASE, X, ...) CASE(X) CASE_F_17(CASE, __VA_ARGS__)
203
#define CASE_F_19(CASE, X, ...) CASE(X) CASE_F_18(CASE, __VA_ARGS__)
204
#define CASE_F_20(CASE, X, ...) CASE(X) CASE_F_19(CASE, __VA_ARGS__)
205
206
0
#define GET_CASE_F(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,NAME,...) NAME
207
#define FOR_EACH_MAKE_CASE(...) \
208
0
        GET_CASE_F(__VA_ARGS__,CASE_F_20,CASE_F_19,CASE_F_18,CASE_F_17,CASE_F_16,CASE_F_15,CASE_F_14,CASE_F_13,CASE_F_12,CASE_F_11, \
209
0
                               CASE_F_10,CASE_F_9,CASE_F_8,CASE_F_7,CASE_F_6,CASE_F_5,CASE_F_4,CASE_F_3,CASE_F_2,CASE_F_1) \
210
0
                   (CASE_F,__VA_ARGS__)
211
212
#define IN_SET(x, ...)                          \
213
0
        ({                                      \
214
0
                bool _found = false;            \
215
0
                /* If the build breaks in the line below, you need to extend the case macros */ \
216
0
                static _unused_ char _static_assert__macros_need_to_be_extended[20 - sizeof((int[]){__VA_ARGS__})/sizeof(int)]; \
217
0
                switch(x) {                     \
218
0
                FOR_EACH_MAKE_CASE(__VA_ARGS__) \
219
0
                        _found = true;          \
220
0
                        break;                  \
221
0
                default:                        \
222
0
                        break;                  \
223
0
                }                               \
224
0
                _found;                         \
225
0
        })
226
227
char hexchar(int x);
228
int unhexchar(char c);
229
char octchar(int x);
230
231
char *hexmem(const void *p, size_t l);
232
233
bool filename_is_valid(const char *p);
234
int tempfn_random(const char *p, char **ret);
235
236
0
static inline bool isempty(const char *p) {
237
0
        return !p || !p[0];
238
0
}
Unexecuted instantiation: fuzz-compress.c:isempty
Unexecuted instantiation: compressor.c:isempty
Unexecuted instantiation: log.c:isempty
Unexecuted instantiation: util.c:isempty
239
240
0
static inline const char *strempty(const char *p) {
241
0
        return p ?: "";
242
0
}
Unexecuted instantiation: fuzz-compress.c:strempty
Unexecuted instantiation: compressor.c:strempty
Unexecuted instantiation: log.c:strempty
Unexecuted instantiation: util.c:strempty
243
244
0
static inline const char *strnone(const char *p) {
245
0
        return p ?: "none";
246
0
}
Unexecuted instantiation: fuzz-compress.c:strnone
Unexecuted instantiation: compressor.c:strnone
Unexecuted instantiation: log.c:strnone
Unexecuted instantiation: util.c:strnone
247
248
0
#define streq(a,b) (strcmp((a),(b)) == 0)
249
250
0
static inline bool streq_ptr(const char *a, const char *b) {
251
0
        if (!a && !b)
252
0
                return true;
253
0
        if (!a || !b)
254
0
                return false;
255
256
0
        return streq(a, b);
257
0
}
Unexecuted instantiation: fuzz-compress.c:streq_ptr
Unexecuted instantiation: compressor.c:streq_ptr
Unexecuted instantiation: log.c:streq_ptr
Unexecuted instantiation: util.c:streq_ptr
258
259
0
static inline const char *strna(const char *p) {
260
0
        return p ?: "n/a";
261
0
}
Unexecuted instantiation: fuzz-compress.c:strna
Unexecuted instantiation: compressor.c:strna
Unexecuted instantiation: log.c:strna
Unexecuted instantiation: util.c:strna
262
263
void hexdump(FILE *f, const void *p, size_t s);
264
char* dirname_malloc(const char *path);
265
266
char *strjoin_real(const char *x, ...) _sentinel_;
267
#define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
268
269
#define LS_FORMAT_MODE_MAX (1+3+3+3+1)
270
char* ls_format_mode(mode_t m, char ret[LS_FORMAT_MODE_MAX]);
271
272
#define LS_FORMAT_CHATTR_MAX 11
273
char *ls_format_chattr(unsigned flags, char ret[LS_FORMAT_CHATTR_MAX]);
274
275
#define LS_FORMAT_FAT_ATTRS_MAX 4
276
char *ls_format_fat_attrs(unsigned flags, char ret[LS_FORMAT_FAT_ATTRS_MAX]);
277
278
int safe_atoi(const char *s, int *ret_i);
279
280
int safe_atou(const char *s, unsigned *ret_u);
281
int safe_atollu(const char *s, unsigned long long *ret_u);
282
int safe_atollx(const char *s, unsigned long long *ret_u);
283
284
0
static inline int safe_atou64(const char *s, uint64_t *ret_u) {
285
0
        return safe_atollu(s, (unsigned long long*) ret_u);
286
0
}
Unexecuted instantiation: fuzz-compress.c:safe_atou64
Unexecuted instantiation: compressor.c:safe_atou64
Unexecuted instantiation: log.c:safe_atou64
Unexecuted instantiation: util.c:safe_atou64
287
288
0
static inline int safe_atou32(const char *s, uint32_t *ret_u) {
289
0
        return safe_atou(s, (unsigned*) ret_u);
290
0
}
Unexecuted instantiation: fuzz-compress.c:safe_atou32
Unexecuted instantiation: compressor.c:safe_atou32
Unexecuted instantiation: log.c:safe_atou32
Unexecuted instantiation: util.c:safe_atou32
291
292
0
static inline int safe_atox64(const char *s, uint64_t *ret_u) {
293
0
        return safe_atollx(s, (unsigned long long*) ret_u);
294
0
}
Unexecuted instantiation: fuzz-compress.c:safe_atox64
Unexecuted instantiation: compressor.c:safe_atox64
Unexecuted instantiation: log.c:safe_atox64
Unexecuted instantiation: util.c:safe_atox64
295
296
int readlinkat_malloc(int fd, const char *p, char **ret);
297
int readlink_malloc(const char *p, char **ret);
298
299
#define strjoina(a, ...)                                                \
300
0
        ({                                                              \
301
0
                const char *_appendees_[] = { a, __VA_ARGS__ };         \
302
0
                char *_d_, *_p_;                                        \
303
0
                int _len_ = 0;                                          \
304
0
                unsigned _i_;                                           \
305
0
                for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
306
0
                        _len_ += strlen(_appendees_[_i_]);              \
307
0
                _p_ = _d_ = alloca(_len_ + 1);                          \
308
0
                for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
309
0
                        _p_ = stpcpy(_p_, _appendees_[_i_]);            \
310
0
                *_p_ = 0;                                               \
311
0
                _d_;                                                    \
312
0
        })
313
314
0
#define WHITESPACE " \t"
315
0
#define NEWLINE "\n\r"
316
317
#define ELEMENTSOF(x)                                                    \
318
0
        __extension__ (__builtin_choose_expr(                            \
319
0
                !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
320
0
                sizeof(x)/sizeof((x)[0]),                                \
321
0
                (void)0))
322
323
char **strv_free(char **l);
324
size_t strv_length(char **l);
325
int strv_push(char ***l, char *value);
326
int strv_consume(char ***l, char *value);
327
int strv_extend(char ***l, const char *value);
328
char *strv_find(char **l, const char *name) _pure_;
329
330
0
#define strv_contains(l, s) (!!strv_find((l), (s)))
331
332
0
static inline bool size_multiply_overflow(size_t size, size_t need) {
333
0
        return need != 0 && size > (SIZE_MAX / need);
334
0
}
Unexecuted instantiation: fuzz-compress.c:size_multiply_overflow
Unexecuted instantiation: compressor.c:size_multiply_overflow
Unexecuted instantiation: log.c:size_multiply_overflow
Unexecuted instantiation: util.c:size_multiply_overflow
335
336
0
_malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {
337
0
        if (_unlikely_(size_multiply_overflow(size, need)))
338
0
                return NULL;
339
0
340
0
        return malloc(size * need);
341
0
}
Unexecuted instantiation: fuzz-compress.c:malloc_multiply
Unexecuted instantiation: compressor.c:malloc_multiply
Unexecuted instantiation: log.c:malloc_multiply
Unexecuted instantiation: util.c:malloc_multiply
342
343
0
_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t size, size_t need) {
344
0
        if (_unlikely_(size_multiply_overflow(size, need)))
345
0
                return NULL;
346
347
0
        return realloc(p, size * need);
348
0
}
Unexecuted instantiation: fuzz-compress.c:realloc_multiply
Unexecuted instantiation: compressor.c:realloc_multiply
Unexecuted instantiation: log.c:realloc_multiply
Unexecuted instantiation: util.c:realloc_multiply
349
350
#define STRV_FOREACH(s, l)                      \
351
0
        for ((s) = (l); (s) && *(s); (s)++)
352
353
int xopendirat(int fd, const char *name, int flags, DIR **ret);
354
355
0
static inline bool dot_or_dot_dot(const char *p) {
356
0
        if (!p)
357
0
                return false;
358
359
0
        if (p[0] != '.')
360
0
                return false;
361
362
0
        if (p[1] == 0)
363
0
                return true;
364
365
0
        if (p[1] != '.')
366
0
                return false;
367
368
0
        return p[2] == 0;
369
0
}
Unexecuted instantiation: fuzz-compress.c:dot_or_dot_dot
Unexecuted instantiation: compressor.c:dot_or_dot_dot
Unexecuted instantiation: log.c:dot_or_dot_dot
Unexecuted instantiation: util.c:dot_or_dot_dot
370
371
void progress(void);
372
373
char *strextend(char **x, ...) _sentinel_;
374
375
#if SIZEOF_UID_T == 4
376
#  define UID_FMT "%" PRIu32
377
#elif SIZEOF_UID_T == 2
378
#  define UID_FMT "%" PRIu16
379
#else
380
#  error Unknown uid_t size
381
#endif
382
383
#if SIZEOF_GID_T == 4
384
#  define GID_FMT "%" PRIu32
385
#elif SIZEOF_GID_T == 2
386
#  define GID_FMT "%" PRIu16
387
#else
388
#  error Unknown gid_t size
389
#endif
390
391
#if SIZEOF_PID_T == 4
392
#  define PID_PRI PRIi32
393
#elif SIZEOF_PID_T == 2
394
#  define PID_PRI PRIi16
395
#else
396
#  error Unknown pid_t size
397
#endif
398
#define PID_FMT "%" PID_PRI
399
400
bool uid_is_valid(uid_t uid);
401
402
0
static inline bool gid_is_valid(gid_t gid) {
403
0
        return uid_is_valid((uid_t) gid);
404
0
}
Unexecuted instantiation: fuzz-compress.c:gid_is_valid
Unexecuted instantiation: compressor.c:gid_is_valid
Unexecuted instantiation: log.c:gid_is_valid
Unexecuted instantiation: util.c:gid_is_valid
405
406
int parse_uid(const char *s, uid_t* ret_uid);
407
408
0
static inline int parse_gid(const char *s, gid_t *ret_gid) {
409
0
        return parse_uid(s, (uid_t*) ret_gid);
410
0
}
Unexecuted instantiation: fuzz-compress.c:parse_gid
Unexecuted instantiation: compressor.c:parse_gid
Unexecuted instantiation: log.c:parse_gid
Unexecuted instantiation: util.c:parse_gid
411
412
#define UID_INVALID ((uid_t) -1)
413
#define GID_INVALID ((gid_t) -1)
414
415
#define ALPHABET_LOWER "abcdefghijklmnopqrstuvwxyz"
416
#define ALPHABET_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
417
#define ALPHABET ALPHABET_LOWER ALPHABET_UPPER
418
#define DIGITS "0123456789"
419
#define HEXDIGITS DIGITS "ABCDEF" "abcdef"
420
421
/* This is a bit more restricted than RFC3986 */
422
#define URL_PROTOCOL_FIRST ALPHABET_LOWER
423
#define URL_PROTOCOL_CHARSET ALPHABET_LOWER DIGITS "+.-"
424
#define HOSTNAME_CHARSET ALPHABET DIGITS "-_.%"
425
426
int wait_for_terminate(pid_t pid, siginfo_t *status);
427
428
0
static inline void safe_close_pair(int pair[2]) {
429
0
        if (!pair)
430
0
                return;
431
0
432
0
        pair[0] = safe_close(pair[0]);
433
0
        pair[1] = safe_close(pair[1]);
434
0
}
Unexecuted instantiation: fuzz-compress.c:safe_close_pair
Unexecuted instantiation: compressor.c:safe_close_pair
Unexecuted instantiation: log.c:safe_close_pair
Unexecuted instantiation: util.c:safe_close_pair
435
436
0
static inline char *startswith(const char *s, const char *prefix) {
437
0
        size_t l;
438
439
0
        l = strlen(prefix);
440
0
        if (strncmp(s, prefix, l) == 0)
441
0
                return (char*) s + l;
442
443
0
        return NULL;
444
0
}
Unexecuted instantiation: fuzz-compress.c:startswith
Unexecuted instantiation: compressor.c:startswith
Unexecuted instantiation: log.c:startswith
Unexecuted instantiation: util.c:startswith
445
446
0
static inline bool strv_isempty(char **l) {
447
0
        return !l || !l[0];
448
0
}
Unexecuted instantiation: fuzz-compress.c:strv_isempty
Unexecuted instantiation: compressor.c:strv_isempty
Unexecuted instantiation: log.c:strv_isempty
Unexecuted instantiation: util.c:strv_isempty
449
450
#if !HAVE_RENAMEAT2
451
#  ifndef __NR_renameat2
452
#    if defined __x86_64__
453
#      define __NR_renameat2 316
454
#    elif defined __alpha__
455
#      define __NR_renameat2 510
456
#    elif defined __arm__
457
#      define __NR_renameat2 382
458
#    elif defined __aarch64__
459
#      define __NR_renameat2 276
460
#    elif defined __hppa__
461
#      define __NR_renameat2 337
462
#    elif defined __ia64__
463
#      define __NR_renameat2 1338
464
#    elif defined __m68k__
465
#      define __NR_renameat2 351
466
#    elif defined _MIPS_SIM
467
#      if _MIPS_SIM == _MIPS_SIM_ABI32
468
#        define __NR_renameat2 4351
469
#      endif
470
#      if _MIPS_SIM == _MIPS_SIM_NABI32
471
#        define __NR_renameat2 6315
472
#      endif
473
#      if _MIPS_SIM == _MIPS_SIM_ABI64
474
#        define __NR_renameat2 5311
475
#      endif
476
#    elif defined __i386__
477
#      define __NR_renameat2 353
478
#    elif defined __powerpc64__ || defined __powerpc__
479
#      define __NR_renameat2 357
480
#    elif defined __s390__ || defined __s390x__
481
#      define __NR_renameat2 347
482
#    elif defined __sh__
483
#      define __NR_renameat2 371
484
#    elif defined __sh64__
485
#      define __NR_renameat2 382
486
#    elif defined __sparc__
487
#      define __NR_renameat2 345
488
#    elif defined __arc__
489
#      define __NR_renameat2 276
490
#    else
491
#      warning "__NR_renameat2 unknown for your architecture"
492
#    endif
493
#  endif
494
495
static inline int renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
496
#  ifdef __NR_renameat2
497
        return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
498
#  else
499
        errno = ENOSYS;
500
        return -1;
501
#  endif
502
}
503
#endif
504
505
#ifndef RENAME_NOREPLACE
506
#define RENAME_NOREPLACE (1 << 0)
507
#endif
508
509
0
static inline size_t strlen_null(const char *s) {
510
0
        if (!s)
511
0
                return 0;
512
0
513
0
        return strlen(s);
514
0
}
Unexecuted instantiation: fuzz-compress.c:strlen_null
Unexecuted instantiation: compressor.c:strlen_null
Unexecuted instantiation: log.c:strlen_null
Unexecuted instantiation: util.c:strlen_null
515
516
0
#define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL }))
517
518
#define FOREACH_STRING(x, y, ...)                                       \
519
0
        for (char **_l = STRV_MAKE(({ x = y; }), ##__VA_ARGS__);        \
520
0
             x;                                                         \
521
0
             x = *(++_l))
522
523
0
#define STR_IN_SET(x, ...) strv_contains(STRV_MAKE(__VA_ARGS__), x)
524
525
0
static inline const char *empty_or_dash_to_null(const char *s) {
526
0
        if (isempty(s))
527
0
                return NULL;
528
0
        if (streq(s, "-"))
529
0
                return NULL;
530
0
531
0
        return s;
532
0
}
Unexecuted instantiation: fuzz-compress.c:empty_or_dash_to_null
Unexecuted instantiation: compressor.c:empty_or_dash_to_null
Unexecuted instantiation: log.c:empty_or_dash_to_null
Unexecuted instantiation: util.c:empty_or_dash_to_null
533
534
size_t page_size(void);
535
536
0
static inline size_t ALIGN_TO(size_t l, size_t ali) {
537
0
        return ((l + ali - 1) & ~(ali - 1));
538
0
}
Unexecuted instantiation: fuzz-compress.c:ALIGN_TO
Unexecuted instantiation: compressor.c:ALIGN_TO
Unexecuted instantiation: log.c:ALIGN_TO
Unexecuted instantiation: util.c:ALIGN_TO
539
#define PAGE_ALIGN(l) ALIGN_TO((l), page_size())
540
541
/* We align a bit more than necessary on 32bit arches */
542
#define ALIGN8(l) (((l) + 7) & ~7)
543
#define ALIGN(l) ALIGN8(l)
544
545
int parse_boolean(const char *v);
546
547
int getenv_bool(const char *p);
548
549
0
#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
550
551
typedef typeof(((struct statfs*)NULL)->f_type) statfs_f_type_t;
552
553
0
static inline bool F_TYPE_EQUAL(statfs_f_type_t a, statfs_f_type_t b) {
554
0
        return a == b;
555
0
}
Unexecuted instantiation: fuzz-compress.c:F_TYPE_EQUAL
Unexecuted instantiation: compressor.c:F_TYPE_EQUAL
Unexecuted instantiation: log.c:F_TYPE_EQUAL
Unexecuted instantiation: util.c:F_TYPE_EQUAL
556
557
0
static inline bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
558
0
        assert(s);
559
0
        return F_TYPE_EQUAL(s->f_type, magic_value);
560
0
}
Unexecuted instantiation: fuzz-compress.c:is_fs_type
Unexecuted instantiation: compressor.c:is_fs_type
Unexecuted instantiation: log.c:is_fs_type
Unexecuted instantiation: util.c:is_fs_type
561
562
0
static inline bool is_temporary_fs(const struct statfs *s) {
563
0
    return is_fs_type(s, TMPFS_MAGIC) ||
564
0
           is_fs_type(s, RAMFS_MAGIC);
565
0
}
Unexecuted instantiation: fuzz-compress.c:is_temporary_fs
Unexecuted instantiation: compressor.c:is_temporary_fs
Unexecuted instantiation: log.c:is_temporary_fs
Unexecuted instantiation: util.c:is_temporary_fs
566
567
#define IS_POWER_OF_TWO(x) (__builtin_popcount(x) == 1)
568
569
0
static inline const char *yes_no(bool b) {
570
0
        return b ? "yes" : "no";
571
0
}
Unexecuted instantiation: fuzz-compress.c:yes_no
Unexecuted instantiation: compressor.c:yes_no
Unexecuted instantiation: log.c:yes_no
Unexecuted instantiation: util.c:yes_no
572
573
#ifndef XFS_SUPER_MAGIC
574
#define XFS_SUPER_MAGIC 0x58465342
575
#endif
576
577
#ifndef CONFIGFS_MAGIC
578
#define CONFIGFS_MAGIC 0x62656570
579
#endif
580
581
#ifndef MQUEUE_MAGIC
582
#define MQUEUE_MAGIC 0x19800202
583
#endif
584
585
#ifndef RPCAUTH_GSSMAGIC
586
#define RPCAUTH_GSSMAGIC 0x67596969
587
#endif
588
589
#ifndef NFSD_MAGIC
590
#define NFSD_MAGIC 0x6e667364
591
#endif
592
593
#ifndef FUSE_CTL_SUPER_MAGIC
594
#define FUSE_CTL_SUPER_MAGIC 0x65735543
595
#endif
596
597
#ifndef CGROUP2_SUPER_MAGIC
598
#define CGROUP2_SUPER_MAGIC 0x63677270
599
#endif
600
601
#ifndef FUSE_SUPER_MAGIC
602
#define FUSE_SUPER_MAGIC 0x65735546
603
#endif
604
605
#ifndef FICLONERANGE
606
struct file_clone_range {
607
        int64_t src_fd;
608
        uint64_t src_offset;
609
        uint64_t src_length;
610
        uint64_t dest_offset;
611
};
612
613
#define FICLONERANGE _IOW(0x94, 13, struct file_clone_range)
614
#endif
615
616
#define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
617
#define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
618
#define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
619
#define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
620
621
void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
622
void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
623
624
#define GREEDY_REALLOC(array, allocated, need)                          \
625
0
        greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
626
627
#define GREEDY_REALLOC0(array, allocated, need)                         \
628
        greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
629
630
#define alloca0(n)                                      \
631
0
        ({                                              \
632
0
                char *_new_;                            \
633
0
                size_t _len_ = n;                       \
634
0
                _new_ = alloca(_len_);                  \
635
0
                (void *) memset(_new_, 0, _len_);       \
636
0
        })
637
638
0
#define memzero(x,l) (memset((x), 0, (l)))
639
#define zero(x) (memzero(&(x), sizeof(x)))
640
#define malloc0(n) (calloc(1, (n)))
641
642
0
static inline void *mempset(void *s, int c, size_t n) {
643
0
        memset(s, c, n);
644
0
        return (uint8_t*)s + n;
645
0
}
Unexecuted instantiation: fuzz-compress.c:mempset
Unexecuted instantiation: compressor.c:mempset
Unexecuted instantiation: log.c:mempset
Unexecuted instantiation: util.c:mempset
646
647
#define DECIMAL_STR_MAX(type)                                           \
648
        (1+(sizeof(type) <= 1 ? 3 :                                     \
649
            sizeof(type) <= 2 ? 5 :                                     \
650
            sizeof(type) <= 4 ? 10 :                                    \
651
            sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
652
653
int skip_bytes_fd(int fd, uint64_t n_bytes);
654
655
char *truncate_nl(char *p);
656
657
#define SOCKADDR_UN_LEN(sa)                                             \
658
        ({                                                              \
659
                const struct sockaddr_un *_sa = &(sa);                  \
660
                assert(_sa->sun_family == AF_UNIX);                     \
661
                offsetof(struct sockaddr_un, sun_path) +                \
662
                        (_sa->sun_path[0] == 0 ?                        \
663
                         1 + strnlen(_sa->sun_path+1, sizeof(_sa->sun_path)-1) : \
664
                         strnlen(_sa->sun_path, sizeof(_sa->sun_path))); \
665
        })
666
667
0
static inline uint32_t rol32(uint32_t x, size_t i) {
668
0
        i %= 32;
669
0
670
0
        if (i == 0) /* Make ubsan happy */
671
0
                return x;
672
0
673
0
        return ((x) << (i)) | ((x) >> (32 - i));
674
0
}
Unexecuted instantiation: fuzz-compress.c:rol32
Unexecuted instantiation: compressor.c:rol32
Unexecuted instantiation: log.c:rol32
Unexecuted instantiation: util.c:rol32
675
676
0
static inline unsigned log2u(unsigned x) {
677
0
        assert(x > 0);
678
0
679
0
        return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
680
0
}
Unexecuted instantiation: fuzz-compress.c:log2u
Unexecuted instantiation: compressor.c:log2u
Unexecuted instantiation: log.c:log2u
Unexecuted instantiation: util.c:log2u
681
682
0
static inline unsigned log2u_round_up(unsigned x) {
683
0
        assert(x > 0);
684
0
685
0
        if (x == 1)
686
0
                return 0;
687
0
688
0
        return log2u(x - 1) + 1;
689
0
}
Unexecuted instantiation: fuzz-compress.c:log2u_round_up
Unexecuted instantiation: compressor.c:log2u_round_up
Unexecuted instantiation: log.c:log2u_round_up
Unexecuted instantiation: util.c:log2u_round_up
690
691
#ifndef FS_PROJINHERIT_FL
692
#define FS_PROJINHERIT_FL 0x20000000
693
#endif
694
695
#ifndef RENAME_EXCHANGE
696
#define RENAME_EXCHANGE (1 << 1)
697
#endif
698
699
int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
700
701
char* path_startswith(const char *path, const char *prefix);
702
703
0
static inline bool path_is_absolute(const char *p) {
704
0
        return p && p[0] == '/';
705
0
}
Unexecuted instantiation: fuzz-compress.c:path_is_absolute
Unexecuted instantiation: compressor.c:path_is_absolute
Unexecuted instantiation: log.c:path_is_absolute
Unexecuted instantiation: util.c:path_is_absolute
706
707
int var_tmp_dir(const char **ret);
708
int tmp_dir(const char **ret);
709
710
bool path_is_safe(const char *p);
711
712
int is_dir(const char* path, bool follow);
713
714
#ifndef BTRFS_IOC_SUBVOL_GETFLAGS
715
#define BTRFS_IOC_SUBVOL_GETFLAGS _IOR(BTRFS_IOCTL_MAGIC, 25, __u64)
716
#endif
717
718
#ifndef BTRFS_IOC_SUBVOL_SETFLAGS
719
#define BTRFS_IOC_SUBVOL_SETFLAGS _IOW(BTRFS_IOCTL_MAGIC, 26, __u64)
720
#endif
721
722
#ifndef FS_IOC_FSGETXATTR
723
struct fsxattr {
724
        uint32_t fsx_xflags;
725
        uint32_t fsx_extsize;
726
        uint32_t fsx_nextents;
727
        uint32_t fsx_projid;
728
        uint32_t fsx_cowextsize;
729
        uint8_t  fsx_pad[8];
730
};
731
#define FS_IOC_FSGETXATTR _IOR ('X', 31, struct fsxattr)
732
#define FS_IOC_FSSETXATTR _IOW ('X', 32, struct fsxattr)
733
#endif
734
735
/* Cleanup functions */
736
737
429
#define _cleanup_(x) __attribute__((cleanup(x)))
738
739
#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func)                 \
740
0
        static inline void func##p(type *p) {                   \
741
0
                if (*p)                                         \
742
0
                        func(*p);                               \
743
0
        }                                                       \
Unexecuted instantiation: fuzz-compress.c:unlink_and_freep
Unexecuted instantiation: compressor.c:unlink_and_freep
Unexecuted instantiation: log.c:unlink_and_freep
Unexecuted instantiation: util.c:funlockfilep
Unexecuted instantiation: util.c:unlink_and_freep
744
        struct __useless_struct_to_allow_trailing_semicolon__
745
746
219
static inline void freep(void *p) {
747
219
        free(*(void**) p);
748
219
}
fuzz-compress.c:freep
Line
Count
Source
746
219
static inline void freep(void *p) {
747
219
        free(*(void**) p);
748
219
}
Unexecuted instantiation: compressor.c:freep
Unexecuted instantiation: log.c:freep
Unexecuted instantiation: util.c:freep
749
750
219
#define _cleanup_free_ _cleanup_(freep)
751
752
0
static inline void unlink_and_free(char *p) {
753
0
        int saved_errno;
754
0
755
0
        if (!p)
756
0
                return;
757
0
758
0
        saved_errno = errno;
759
0
        (void) unlink(p);
760
0
        errno = saved_errno;
761
0
762
0
        free(p);
763
0
}
Unexecuted instantiation: fuzz-compress.c:unlink_and_free
Unexecuted instantiation: compressor.c:unlink_and_free
Unexecuted instantiation: log.c:unlink_and_free
Unexecuted instantiation: util.c:unlink_and_free
764
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
765
766
int free_and_strdup(char **p, const char *s);
767
768
/* A check against a list of errors commonly used to indicate that a syscall/ioctl/other kernel operation we request is
769
 * not supported locally. We maintain a generic list for this here, instead of adjusting the possible error codes to
770
 * exactly what the calls might return for the simple reasons that due to FUSE and many differing in-kernel
771
 * implementations of the same calls in various file systems and such the error codes seen varies wildly, and we'd like
772
 * to cover them all to some degree and be somewhat safe for the future too. */
773
#define ERRNO_IS_UNSUPPORTED(error) \
774
0
        IN_SET(error, ENOTTY, ENOSYS, EBADF, EOPNOTSUPP, EINVAL)
775
776
#define LARGE_LINE_MAX (64U*1024U)
777
778
int read_line(FILE *f, size_t limit, char **ret);
779
780
char *delete_trailing_chars(char *s, const char *bad);
781
char *strstrip(char *s);
782
783
#endif