Coverage Report

Created: 2025-07-12 06:52

/src/zstd/tests/fuzz/simple_decompress.c
Line
Count
Source (jump to first uncovered line)
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 <stddef.h>
17
#include <stdlib.h>
18
#include <stdio.h>
19
20
#define ZSTD_STATIC_LINKING_ONLY
21
22
#include "fuzz_helpers.h"
23
#include "zstd.h"
24
#include "fuzz_data_producer.h"
25
26
static ZSTD_DCtx *dctx = NULL;
27
28
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
29
17.5k
{
30
    /* Give a random portion of src data to the producer, to use for
31
    parameter generation. The rest will be used for (de)compression */
32
17.5k
    FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
33
17.5k
    size = FUZZ_dataProducer_reserveDataPrefix(producer);
34
35
17.5k
    if (!dctx) {
36
17.5k
        dctx = ZSTD_createDCtx();
37
17.5k
        FUZZ_ASSERT(dctx);
38
17.5k
    }
39
40
17.5k
    {
41
17.5k
        size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, 10 * size);
42
17.5k
        void *rBuf = FUZZ_malloc(bufSize);
43
17.5k
        size_t const dSize = ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
44
17.5k
        if (!ZSTD_isError(dSize)) {
45
            /* If decompression was successful, the content size from the frame header(s) should be valid. */
46
1.24k
            unsigned long long const expectedSize = ZSTD_findDecompressedSize(src, size);
47
1.24k
            FUZZ_ASSERT(expectedSize != ZSTD_CONTENTSIZE_ERROR);
48
1.24k
            FUZZ_ASSERT(expectedSize == ZSTD_CONTENTSIZE_UNKNOWN || expectedSize == dSize);
49
1.24k
        }
50
17.5k
        free(rBuf);
51
17.5k
    }
52
53
0
    FUZZ_dataProducer_free(producer);
54
55
17.5k
#ifndef STATEFUL_FUZZING
56
17.5k
    ZSTD_freeDCtx(dctx); dctx = NULL;
57
17.5k
#endif
58
17.5k
    return 0;
59
17.5k
}