1 /**
2 * This fuzz target attempts to compress the fuzzed data with the simple
3 * compression function with an output buffer that may be too small to
4 * ensure that the compressor never crashes.
5 */
6
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "fuzz_helpers.h"
13 #include "lz4.h"
14 #include "lz4frame.h"
15 #include "lz4_helpers.h"
16
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)17 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
18 {
19 uint32_t seed = FUZZ_seed(&data, &size);
20 LZ4F_preferences_t const prefs = FUZZ_randomPreferences(&seed);
21 size_t const compressBound = LZ4F_compressFrameBound(size, &prefs);
22 size_t const dstCapacity = FUZZ_rand32(&seed, 0, compressBound);
23 char* const dst = (char*)malloc(dstCapacity);
24 char* const rt = (char*)malloc(size);
25
26 FUZZ_ASSERT(dst);
27 FUZZ_ASSERT(rt);
28
29 /* If compression succeeds it must round trip correctly. */
30 size_t const dstSize =
31 LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs);
32 if (!LZ4F_isError(dstSize)) {
33 size_t const rtSize = FUZZ_decompressFrame(rt, size, dst, dstSize);
34 FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size");
35 FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
36 }
37
38 free(dst);
39 free(rt);
40
41 return 0;
42 }
43