]>
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 static int compress(int alg
,
10 const void *src
, uint64_t src_size
,
11 void *dst
, size_t dst_alloc_size
, size_t *dst_size
) {
13 if (alg
== OBJECT_COMPRESSED_LZ4
)
14 return compress_blob_lz4(src
, src_size
, dst
, dst_alloc_size
, dst_size
);
15 if (alg
== OBJECT_COMPRESSED_XZ
)
16 return compress_blob_xz(src
, src_size
, dst
, dst_alloc_size
, dst_size
);
20 typedef struct header
{
21 uint32_t alg
:2; /* We have only two compression algorithms so far, but we might add
22 * more in the future. Let's make this a bit wider so our fuzzer
23 * cases remain stable in the future. */
26 uint32_t reserved
[3]; /* Extra space to keep fuzz cases stable in case we need to
27 * add stuff in the future. */
31 int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
) {
32 _cleanup_free_
void *buf
= NULL
, *buf2
= NULL
;
35 if (size
< offsetof(header
, data
) + 1)
38 const header
*h
= (struct header
*) data
;
39 const size_t data_len
= size
- offsetof(header
, data
);
43 /* We don't want to fill the logs with messages about parse errors.
44 * Disable most logging if not running standalone */
45 if (!getenv("SYSTEMD_LOG_LEVEL"))
46 log_set_max_level(LOG_CRIT
);
48 log_info("Using compression %s, data size=%zu",
49 object_compressed_to_string(alg
) ?: "(none)",
52 buf
= malloc(MAX(size
, 128u)); /* Make the buffer a bit larger for very small data */
59 r
= compress(alg
, h
->data
, data_len
, buf
, size
, &csize
);
61 log_error_errno(r
, "Compression failed: %m");
65 log_debug("Compressed %zu bytes to → %zu bytes", data_len
, csize
);
67 size_t sw_alloc
= MAX(h
->sw_alloc
, 1u);
68 buf2
= malloc(sw_alloc
);
74 size_t sw_len
= MIN(data_len
- 1, h
->sw_len
);
76 r
= decompress_startswith(alg
, buf
, csize
, &buf2
, &sw_alloc
, h
->data
, sw_len
, h
->data
[sw_len
]);