]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/zstd/tests/fuzz/simple_decompress.c
Update to Zstandard 1.4.4
[FreeBSD/FreeBSD.git] / sys / contrib / zstd / tests / fuzz / simple_decompress.c
1 /*
2  * Copyright (c) 2016-present, Facebook, Inc.
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  */
9
10 /**
11  * This fuzz target attempts to decompress the fuzzed data with the simple
12  * decompression function to ensure the decompressor never crashes.
13  */
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include "fuzz_helpers.h"
19 #include "zstd.h"
20 #include "fuzz_data_producer.h"
21
22 static ZSTD_DCtx *dctx = NULL;
23
24 int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
25 {
26     /* Give a random portion of src data to the producer, to use for
27     parameter generation. The rest will be used for (de)compression */
28     FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
29     size = FUZZ_dataProducer_reserveDataPrefix(producer);
30
31     if (!dctx) {
32         dctx = ZSTD_createDCtx();
33         FUZZ_ASSERT(dctx);
34     }
35
36     size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, 10 * size);
37     void *rBuf = malloc(bufSize);
38     FUZZ_ASSERT(rBuf);
39
40     ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
41     free(rBuf);
42
43     FUZZ_dataProducer_free(producer);
44
45 #ifndef STATEFUL_FUZZING
46     ZSTD_freeDCtx(dctx); dctx = NULL;
47 #endif
48     return 0;
49 }