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