]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/fuzz/fuzz-compress.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
9 typedef struct header
{
10 uint32_t alg
:2; /* We have only three compression algorithms so far, but we might add more in the
11 * future. Let's make this a bit wider so our fuzzer cases remain stable in the
15 uint32_t reserved
[3]; /* Extra space to keep fuzz cases stable in case we need to
16 * add stuff in the future. */
20 int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
) {
21 _cleanup_free_
void *buf
= NULL
, *buf2
= NULL
;
24 if (size
< offsetof(header
, data
) + 1)
27 const header
*h
= (struct header
*) data
;
28 const size_t data_len
= size
- offsetof(header
, data
);
32 /* We don't want to fill the logs with messages about parse errors.
33 * Disable most logging if not running standalone */
34 if (!getenv("SYSTEMD_LOG_LEVEL"))
35 log_set_max_level(LOG_CRIT
);
37 log_info("Using compression %s, data size=%zu",
38 compression_to_string(alg
),
41 buf
= malloc(MAX(size
, 128u)); /* Make the buffer a bit larger for very small data */
48 r
= compress_blob_explicit(alg
, h
->data
, data_len
, buf
, size
, &csize
);
50 log_error_errno(r
, "Compression failed: %m");
54 log_debug("Compressed %zu bytes to → %zu bytes", data_len
, csize
);
56 size_t sw_alloc
= MAX(h
->sw_alloc
, 1u);
57 buf2
= malloc(sw_alloc
);
63 size_t sw_len
= MIN(data_len
- 1, h
->sw_len
);
65 r
= decompress_startswith(alg
, buf
, csize
, &buf2
, h
->data
, sw_len
, h
->data
[sw_len
]);