1
0

chunked_decoder_fuzzer.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <cstdint>
  2. #include "fuzzed_stream.h"
  3. namespace {
  4. void decode(const uint8_t *data, size_t size, size_t payload_max_length) {
  5. FuzzedStream strm(data, size);
  6. httplib::Response res;
  7. httplib::detail::read_content_chunked(
  8. strm, res, payload_max_length,
  9. [](const char *, size_t, uint64_t, uint64_t) { return true; });
  10. }
  11. } // namespace
  12. // The first decode initializes function-local statics, which in a persistent
  13. // fuzzer would otherwise show up as unstable coverage on the first input only.
  14. extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) {
  15. static const char warm_up[] =
  16. "5;ext=1\r\nhello\r\n0\r\nX-Trailer: 1\r\nExpires: 0\r\n\r\n";
  17. decode(reinterpret_cast<const uint8_t *>(warm_up), sizeof(warm_up) - 1, 0);
  18. return 0;
  19. }
  20. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  21. if (size < 2) { return 0; }
  22. // The first byte selects the payload limit, 0 meaning none
  23. static const size_t max_lens[] = {0, 1, 16, 4096,
  24. CPPHTTPLIB_PAYLOAD_MAX_LENGTH};
  25. auto n = sizeof(max_lens) / sizeof(max_lens[0]);
  26. decode(data + 1, size - 1, max_lens[data[0] % n]);
  27. return 0;
  28. }