one-arg.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2019, Paul Dreik
  2. // For the license information refer to format.h.
  3. #include <fmt/chrono.h>
  4. #include <cstdint>
  5. #include <exception>
  6. #include "fuzzer-common.h"
  7. template <typename T, typename Repr> const T* from_repr(const Repr& r) {
  8. return &r;
  9. }
  10. template <> const std::tm* from_repr<std::tm>(const std::time_t& t) {
  11. return std::localtime(&t);
  12. }
  13. template <typename T, typename Repr = T>
  14. void invoke_fmt(const uint8_t* data, size_t size) {
  15. static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
  16. if (size <= fixed_size) return;
  17. auto repr = assign_from_buf<Repr>(data);
  18. const T* value = from_repr<T>(repr);
  19. if (!value) return;
  20. data += fixed_size;
  21. size -= fixed_size;
  22. data_to_string format_str(data, size);
  23. try {
  24. #if FMT_FUZZ_FORMAT_TO_STRING
  25. std::string message = fmt::format(format_str.get(), *value);
  26. #else
  27. auto buf = fmt::memory_buffer();
  28. fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
  29. #endif
  30. } catch (std::exception&) {
  31. }
  32. }
  33. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  34. if (size <= 3) return 0;
  35. const auto first = data[0];
  36. data++;
  37. size--;
  38. switch (first) {
  39. case 0:
  40. invoke_fmt<bool>(data, size);
  41. break;
  42. case 1:
  43. invoke_fmt<char>(data, size);
  44. break;
  45. case 2:
  46. invoke_fmt<unsigned char>(data, size);
  47. break;
  48. case 3:
  49. invoke_fmt<signed char>(data, size);
  50. break;
  51. case 4:
  52. invoke_fmt<short>(data, size);
  53. break;
  54. case 5:
  55. invoke_fmt<unsigned short>(data, size);
  56. break;
  57. case 6:
  58. invoke_fmt<int>(data, size);
  59. break;
  60. case 7:
  61. invoke_fmt<unsigned int>(data, size);
  62. break;
  63. case 8:
  64. invoke_fmt<long>(data, size);
  65. break;
  66. case 9:
  67. invoke_fmt<unsigned long>(data, size);
  68. break;
  69. case 10:
  70. invoke_fmt<float>(data, size);
  71. break;
  72. case 11:
  73. invoke_fmt<double>(data, size);
  74. break;
  75. case 12:
  76. invoke_fmt<long double>(data, size);
  77. break;
  78. case 13:
  79. invoke_fmt<std::tm, std::time_t>(data, size);
  80. break;
  81. }
  82. return 0;
  83. }