block_decompress.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Copyright (c) 2016-2021, 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. * You may select, at your option, one of the above-listed licenses.
  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. #define ZSTD_STATIC_LINKING_ONLY
  15. #include <stddef.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include "fuzz_helpers.h"
  19. #include "zstd.h"
  20. static ZSTD_DCtx *dctx = NULL;
  21. static void* rBuf = NULL;
  22. static size_t bufSize = 0;
  23. int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
  24. {
  25. size_t const neededBufSize = ZSTD_BLOCKSIZE_MAX;
  26. /* Allocate all buffers and contexts if not already allocated */
  27. if (neededBufSize > bufSize) {
  28. free(rBuf);
  29. rBuf = FUZZ_malloc(neededBufSize);
  30. bufSize = neededBufSize;
  31. }
  32. if (!dctx) {
  33. dctx = ZSTD_createDCtx();
  34. FUZZ_ASSERT(dctx);
  35. }
  36. ZSTD_decompressBegin(dctx);
  37. ZSTD_decompressBlock(dctx, rBuf, neededBufSize, src, size);
  38. #ifndef STATEFUL_FUZZING
  39. ZSTD_freeDCtx(dctx); dctx = NULL;
  40. #endif
  41. return 0;
  42. }