/src/zstd/tests/fuzz/block_decompress.c
Line | Count | Source |
1 | | /** |
2 | | * Copyright (c) 2016-present, Yann Collet, 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 | | #define ZSTD_STATIC_LINKING_ONLY |
16 | | |
17 | | #include <stddef.h> |
18 | | #include <stdlib.h> |
19 | | #include <stdio.h> |
20 | | #include "fuzz_helpers.h" |
21 | | #include "zstd.h" |
22 | | |
23 | | static ZSTD_DCtx *dctx = NULL; |
24 | | static void* rBuf = NULL; |
25 | | static size_t bufSize = 0; |
26 | | |
27 | | int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size) |
28 | 2.16k | { |
29 | 2.16k | size_t const neededBufSize = ZSTD_BLOCKSIZE_MAX; |
30 | 2.16k | |
31 | 2.16k | FUZZ_seed(&src, &size); |
32 | 2.16k | |
33 | 2.16k | /* Allocate all buffers and contexts if not already allocated */ |
34 | 2.16k | if (neededBufSize > bufSize) { |
35 | 1 | free(rBuf); |
36 | 1 | rBuf = malloc(neededBufSize); |
37 | 1 | bufSize = neededBufSize; |
38 | 1 | FUZZ_ASSERT(rBuf); |
39 | 1 | } |
40 | 2.16k | if (!dctx) { |
41 | 2.16k | dctx = ZSTD_createDCtx(); |
42 | 2.16k | FUZZ_ASSERT(dctx); |
43 | 2.16k | } |
44 | 2.16k | ZSTD_decompressBegin(dctx); |
45 | 2.16k | ZSTD_decompressBlock(dctx, rBuf, neededBufSize, src, size); |
46 | 2.16k | |
47 | 2.16k | #ifndef STATEFUL_FUZZING |
48 | 2.16k | ZSTD_freeDCtx(dctx); dctx = NULL; |
49 | 2.16k | #endif |
50 | 2.16k | return 0; |
51 | 2.16k | } |