Coverage Report

Created: 2025-07-11 06:33

/src/zstd/tests/fuzz/block_decompress.c
Line
Count
Source
1
/**
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under both the BSD-style license (found in the
6
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
 * in the COPYING file in the root directory of this source tree).
8
 * You may select, at your option, one of the above-listed licenses.
9
 */
10
11
/**
12
 * This fuzz target attempts to decompress the fuzzed data with the simple
13
 * decompression function to ensure the decompressor never crashes.
14
 */
15
16
#include "fuzz_data_producer.h"
17
#define ZSTD_STATIC_LINKING_ONLY
18
19
#include <stddef.h>
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include "fuzz_helpers.h"
23
#include "zstd.h"
24
25
static ZSTD_DCtx *dctx = NULL;
26
static void* rBuf = NULL;
27
static size_t bufSize = 0;
28
29
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
30
3.70k
{
31
3.70k
    size_t const neededBufSize = ZSTD_BLOCKSIZE_MAX;
32
3.70k
    FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
33
34
    /* Allocate all buffers and contexts if not already allocated */
35
3.70k
    if (neededBufSize > bufSize) {
36
1
        free(rBuf);
37
1
        rBuf = FUZZ_malloc_rand(neededBufSize, producer);
38
1
        bufSize = neededBufSize;
39
1
    }
40
3.70k
    if (!dctx) {
41
3.70k
        dctx = ZSTD_createDCtx();
42
3.70k
        FUZZ_ASSERT(dctx);
43
3.70k
    }
44
3.70k
    ZSTD_decompressBegin(dctx);
45
3.70k
    ZSTD_decompressBlock(dctx, rBuf, neededBufSize, src, size);
46
47
3.70k
    FUZZ_dataProducer_free(producer);
48
49
3.70k
#ifndef STATEFUL_FUZZING
50
3.70k
    ZSTD_freeDCtx(dctx); dctx = NULL;
51
3.70k
#endif
52
3.70k
    return 0;
53
3.70k
}