1 /**
2  * This fuzz target performs a lz4 round-trip test (compress & decompress),
3  * compares the result with the original, and calls abort() on corruption.
4  */
5 
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include "fuzz_helpers.h"
12 #include "lz4.h"
13 #include "lz4hc.h"
14 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)15 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
16 {
17     uint32_t seed = FUZZ_seed(&data, &size);
18     size_t const dstCapacity = LZ4_compressBound(size);
19     char* const dst = (char*)malloc(dstCapacity);
20     char* const rt = (char*)malloc(size);
21     int const level = FUZZ_rand32(&seed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX);
22 
23     FUZZ_ASSERT(dst);
24     FUZZ_ASSERT(rt);
25 
26     /* Compression must succeed and round trip correctly. */
27     int const dstSize = LZ4_compress_HC((const char*)data, dst, size,
28                                         dstCapacity, level);
29     FUZZ_ASSERT(dstSize > 0);
30 
31     int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size);
32     FUZZ_ASSERT_MSG(rtSize == size, "Incorrect size");
33     FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
34 
35     free(dst);
36     free(rt);
37 
38     return 0;
39 }
40