/src/c-blosc2/tests/fuzz/fuzz_compress_chunk.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include <stdint.h> |
2 | | #include <stdlib.h> |
3 | | |
4 | | #include <blosc2.h> |
5 | | |
6 | 14.0k | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
7 | | |
8 | | #ifdef __cplusplus |
9 | | extern "C" { |
10 | | #endif |
11 | | |
12 | 14.0k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
13 | 14.0k | const char *const compressors[] = { "blosclz", "lz4", "lz4hc", "zlib", "zstd" }; |
14 | 14.0k | const int num_comp = ARRAY_SIZE(compressors); |
15 | 14.0k | int level = 9, filter = BLOSC_BITSHUFFLE, cindex = 0, i = 0; |
16 | 14.0k | size_t nbytes, cbytes, blocksize; |
17 | 14.0k | void *output, *input; |
18 | | |
19 | 14.0k | blosc2_set_nthreads(1); |
20 | | |
21 | 14.0k | if (size > 0) |
22 | 14.0k | level = data[0] % (9 + 1); |
23 | 14.0k | if (size > 1) |
24 | 14.0k | filter = data[1] % (BLOSC_BITSHUFFLE + 1); |
25 | 14.0k | if (size > 2) |
26 | 14.0k | cindex = data[2]; |
27 | | |
28 | | /* Find next available compressor */ |
29 | 14.0k | while (blosc1_set_compressor(compressors[cindex % num_comp]) == -1 && i < num_comp) { |
30 | 0 | cindex++, i++; |
31 | 0 | } |
32 | 14.0k | if (i == num_comp) { |
33 | | /* No compressors available */ |
34 | 0 | return 0; |
35 | 0 | } |
36 | | |
37 | 14.0k | if (size > 3 && data[3] % 7 == 0) |
38 | 3.46k | blosc1_set_blocksize(4096); |
39 | | |
40 | 14.0k | output = malloc(size + 1); |
41 | 14.0k | if (output == NULL) |
42 | 0 | return 0; |
43 | | |
44 | 14.0k | int csize = blosc2_compress(level, filter, 1, data, size, output, size); |
45 | 14.0k | if (csize <= 0) { |
46 | | /* Cannot compress src buffer into dest */ |
47 | 5.99k | free(output); |
48 | 5.99k | return 0; |
49 | 5.99k | } |
50 | | |
51 | 8.08k | blosc1_cbuffer_sizes(output, &nbytes, &cbytes, &blocksize); |
52 | | |
53 | | /* Don't allow address sanitizer to allocate more than INT32_MAX */ |
54 | 8.08k | if (cbytes >= INT32_MAX) { |
55 | 0 | free(output); |
56 | 0 | return 0; |
57 | 0 | } |
58 | | |
59 | 8.08k | input = malloc(cbytes); |
60 | 8.08k | if (input != NULL) { |
61 | 8.08k | blosc1_decompress(output, input, cbytes); |
62 | 8.08k | free(input); |
63 | 8.08k | } |
64 | | |
65 | 8.08k | free(output); |
66 | | |
67 | 8.08k | return 0; |
68 | 8.08k | } |
69 | | |
70 | | #ifdef __cplusplus |
71 | | } |
72 | | #endif |