Coverage Report

Created: 2026-11-12 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lz4/ossfuzz/compress_hc_fuzzer.c
Line
Count
Source
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 "fuzz_data_producer.h"
14
#include "lz4.h"
15
#include "lz4hc.h"
16
17
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
18
7.55k
{
19
7.55k
    FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
20
7.55k
    size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
21
7.55k
    size_t const levelSeed = FUZZ_dataProducer_retrieve32(producer);
22
7.55k
    size = FUZZ_dataProducer_remainingBytes(producer);
23
24
7.55k
    size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, size);
25
7.55k
    int const level = FUZZ_getRange_from_uint32(levelSeed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX);
26
27
7.55k
    char* const dst = (char*)malloc(dstCapacity);
28
7.55k
    char* const rt = (char*)malloc(size);
29
30
7.55k
    FUZZ_ASSERT(dst);
31
7.55k
    FUZZ_ASSERT(rt);
32
33
    /* If compression succeeds it must round trip correctly. */
34
7.55k
    {
35
7.55k
        int const dstSize = LZ4_compress_HC((const char*)data, dst, size,
36
7.55k
                                            dstCapacity, level);
37
7.55k
        if (dstSize > 0) {
38
4.01k
            int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size);
39
4.01k
            FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size");
40
4.01k
            FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
41
4.01k
        }
42
7.55k
    }
43
44
7.55k
    if (dstCapacity > 0) {
45
        /* Compression succeeds and must round trip correctly. */
46
7.51k
        void* state = malloc(LZ4_sizeofStateHC());
47
7.51k
        FUZZ_ASSERT(state);
48
7.51k
        int compressedSize = size;
49
7.51k
        int const dstSize = LZ4_compress_HC_destSize(state, (const char*)data,
50
7.51k
                                                     dst, &compressedSize,
51
7.51k
                                                     dstCapacity, level);
52
7.51k
        FUZZ_ASSERT(dstSize > 0);
53
7.51k
        int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size);
54
7.51k
        FUZZ_ASSERT_MSG(rtSize == compressedSize, "Incorrect regenerated size");
55
7.51k
        FUZZ_ASSERT_MSG(!memcmp(data, rt, compressedSize), "Corruption!");
56
7.51k
        free(state);
57
7.51k
    }
58
59
7.55k
    free(dst);
60
7.55k
    free(rt);
61
7.55k
    FUZZ_dataProducer_free(producer);
62
63
7.55k
    return 0;
64
7.55k
}