/src/casync/test/fuzz/fuzz-compress.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | | |
3 | | #include <errno.h> |
4 | | #include <syslog.h> |
5 | | |
6 | | #include "compressor.h" |
7 | | #include "fuzz.h" |
8 | | #include "log.h" |
9 | | #include "util.h" |
10 | | |
11 | | typedef struct header { |
12 | | uint32_t alg; |
13 | | uint32_t reserved[5]; /* Extra space to keep fuzz cases stable in case we need to |
14 | | * add stuff in the future. */ |
15 | | uint8_t data[]; |
16 | | } header; |
17 | | |
18 | 221 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
19 | 221 | _cleanup_free_ void *buf = NULL; |
20 | 221 | int r; |
21 | | |
22 | 221 | if (size < offsetof(header, data) + 1) |
23 | 9 | return 0; |
24 | | |
25 | | /* We don't want to fill the logs with messages about parse errors. |
26 | | * Disable most logging if not running standalone */ |
27 | 212 | if (!getenv("CASYNC_LOG_LEVEL")) |
28 | 212 | set_log_level(LOG_CRIT); |
29 | | |
30 | 212 | const header *h = (struct header*) data; |
31 | 212 | const size_t data_len = size - offsetof(header, data); |
32 | | |
33 | 212 | _cleanup_(compressor_finish) CompressorContext c = COMPRESSOR_CONTEXT_INIT; |
34 | | |
35 | 212 | r = compressor_start_decode(&c, h->alg); |
36 | 212 | if (r < 0) { |
37 | 70 | log_debug_errno(r, "compressor_start_decode failed: %m"); |
38 | 70 | return 0; |
39 | 70 | } |
40 | | |
41 | 142 | log_info("Using compression %d, data size=%zu", h->alg, data_len); |
42 | | |
43 | 142 | size_t out_size = MAX(size, 128u), /* Make the buffer a bit larger for very small data */ |
44 | 142 | ret_done; |
45 | 142 | buf = malloc(out_size); |
46 | 142 | if (!buf) { |
47 | 0 | log_oom(); |
48 | 0 | return 0; |
49 | 0 | } |
50 | | |
51 | 142 | r = compressor_input(&c, h->data, data_len); |
52 | 142 | if (r < 0) { |
53 | 0 | log_debug_errno(r, "compressor_input failed: %m"); |
54 | 0 | return 0; |
55 | 0 | } |
56 | | |
57 | 142 | r = compressor_decode(&c, buf, out_size, &ret_done); |
58 | 142 | if (r < 0) { |
59 | 114 | log_debug_errno(r, "compressor_decode failed: %m"); |
60 | 114 | return 0; |
61 | 114 | } |
62 | | |
63 | 28 | return 0; |
64 | 142 | } |