]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/fuzz/simple_decompress.c
import zstd 1.3.8
[FreeBSD/FreeBSD.git] / 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
21 static ZSTD_DCtx *dctx = NULL;
22 static void* rBuf = NULL;
23 static size_t bufSize = 0;
24
25 int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
26 {
27     size_t neededBufSize;
28
29     FUZZ_seed(&src, &size);
30     neededBufSize = MAX(20 * size, (size_t)256 << 10);
31
32     /* Allocate all buffers and contexts if not already allocated */
33     if (neededBufSize > bufSize) {
34         free(rBuf);
35         rBuf = malloc(neededBufSize);
36         bufSize = neededBufSize;
37         FUZZ_ASSERT(rBuf);
38     }
39     if (!dctx) {
40         dctx = ZSTD_createDCtx();
41         FUZZ_ASSERT(dctx);
42     }
43     ZSTD_decompressDCtx(dctx, rBuf, neededBufSize, src, size);
44
45 #ifndef STATEFUL_FUZZING
46     ZSTD_freeDCtx(dctx); dctx = NULL;
47 #endif
48     return 0;
49 }