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 "lz4frame.h"
14 #include "lz4_helpers.h"
15 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)16 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
17 {
18     uint32_t seed = FUZZ_seed(&data, &size);
19     LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed);
20     size_t const dstCapacity = LZ4F_compressFrameBound(size, &prefs);
21     char* const dst = (char*)malloc(dstCapacity);
22     char* const rt = (char*)malloc(size);
23 
24     FUZZ_ASSERT(dst);
25     FUZZ_ASSERT(rt);
26 
27     /* Compression must succeed and round trip correctly. */
28     size_t const dstSize =
29             LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs);
30     FUZZ_ASSERT(!LZ4F_isError(dstSize));
31     size_t const rtSize = FUZZ_decompressFrame(rt, size, dst, dstSize);
32     FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size");
33     FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
34 
35     free(dst);
36     free(rt);
37 
38     return 0;
39 }
40