multipart_parser_fuzzer.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <cstdint>
  2. #include <string>
  3. #include <httplib.h>
  4. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  5. if (size < 2 || size > 65536) return 0;
  6. // First byte selects the boundary length, the rest is the boundary then body
  7. size_t boundary_len = (static_cast<size_t>(data[0]) % 16) + 1;
  8. if (boundary_len + 1 >= size) boundary_len = 0;
  9. std::string boundary =
  10. boundary_len > 0
  11. ? std::string(reinterpret_cast<const char *>(data + 1), boundary_len)
  12. : "----fuzzboundary";
  13. const uint8_t *body = data + 1 + boundary_len;
  14. size_t body_size = size - 1 - boundary_len;
  15. // FormDataParser::parse, fed in chunks to exercise the streaming paths
  16. httplib::detail::FormDataParser parser;
  17. parser.set_boundary(std::move(boundary));
  18. auto header_cb = [](const httplib::FormData &) -> bool { return true; };
  19. auto content_cb = [](const char *, size_t) -> bool { return true; };
  20. size_t chunk = (static_cast<size_t>(data[1]) % 64) + 1;
  21. for (size_t off = 0; off < body_size; off += chunk) {
  22. size_t n = (off + chunk > body_size) ? body_size - off : chunk;
  23. if (!parser.parse(reinterpret_cast<const char *>(body + off), n, header_cb,
  24. content_cb))
  25. break;
  26. }
  27. return 0;
  28. }