Coverage Report

Created: 2025-07-11 06:47

/src/lz4/ossfuzz/round_trip_frame_fuzzer.c
Line
Count
Source
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
#include "fuzz_data_producer.h"
16
17
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
18
9.17k
{
19
9.17k
    FUZZ_dataProducer_t* producer = FUZZ_dataProducer_create(data, size);
20
9.17k
    LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer);
21
9.17k
    size = FUZZ_dataProducer_remainingBytes(producer);
22
23
9.17k
    size_t const dstCapacity = LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs);
24
9.17k
    char* const dst = (char*)malloc(dstCapacity);
25
9.17k
    char* const rt = (char*)malloc(FUZZ_dataProducer_remainingBytes(producer));
26
27
9.17k
    FUZZ_ASSERT(dst);
28
9.17k
    FUZZ_ASSERT(rt);
29
30
    /* Compression must succeed and round trip correctly. */
31
9.17k
    size_t const dstSize =
32
9.17k
            LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs);
33
9.17k
    FUZZ_ASSERT(!LZ4F_isError(dstSize));
34
9.17k
    size_t const rtSize = FUZZ_decompressFrame(rt, size, dst, dstSize);
35
9.17k
    FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size");
36
9.17k
    FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
37
38
9.17k
    free(dst);
39
9.17k
    free(rt);
40
9.17k
    FUZZ_dataProducer_free(producer);
41
42
9.17k
    return 0;
43
9.17k
}