compress_frame_fuzzer.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * This fuzz target attempts to compress the fuzzed data with the simple
  3. * compression function with an output buffer that may be too small to
  4. * ensure that the compressor never crashes.
  5. */
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "fuzz_helpers.h"
  11. #include "lz4.h"
  12. #include "lz4frame.h"
  13. #include "lz4_helpers.h"
  14. #include "fuzz_data_producer.h"
  15. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
  16. {
  17. FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
  18. LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer);
  19. size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
  20. size = FUZZ_dataProducer_remainingBytes(producer);
  21. size_t const compressBound = LZ4F_compressFrameBound(size, &prefs);
  22. size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, compressBound);
  23. char* const dst = (char*)malloc(dstCapacity);
  24. char* const rt = (char*)malloc(size);
  25. FUZZ_ASSERT(dst!=NULL);
  26. FUZZ_ASSERT(rt!=NULL);
  27. /* If compression succeeds it must round trip correctly. */
  28. size_t const dstSize =
  29. LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs);
  30. if (!LZ4F_isError(dstSize)) {
  31. size_t const rtSize = FUZZ_decompressFrame(rt, size, dst, dstSize);
  32. FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size");
  33. FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
  34. }
  35. free(dst);
  36. free(rt);
  37. FUZZ_dataProducer_free(producer);
  38. return 0;
  39. }