Mercurial > minori
comparison dep/fmt/test/fuzzing/one-arg.cc @ 343:1faa72660932
*: transfer back to cmake from autotools
autotools just made lots of things more complicated than
they should have and many things broke (i.e. translations)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 20 Jun 2024 05:56:06 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
342:adb79bdde329 | 343:1faa72660932 |
---|---|
1 // Copyright (c) 2019, Paul Dreik | |
2 // For the license information refer to format.h. | |
3 | |
4 #include <fmt/chrono.h> | |
5 | |
6 #include <cstdint> | |
7 #include <exception> | |
8 | |
9 #include "fuzzer-common.h" | |
10 | |
11 template <typename T, typename Repr> const T* from_repr(const Repr& r) { | |
12 return &r; | |
13 } | |
14 | |
15 template <> const std::tm* from_repr<std::tm>(const std::time_t& t) { | |
16 return std::localtime(&t); | |
17 } | |
18 | |
19 template <typename T, typename Repr = T> | |
20 void invoke_fmt(const uint8_t* data, size_t size) { | |
21 static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | |
22 if (size <= fixed_size) return; | |
23 auto repr = assign_from_buf<Repr>(data); | |
24 const T* value = from_repr<T>(repr); | |
25 if (!value) return; | |
26 data += fixed_size; | |
27 size -= fixed_size; | |
28 data_to_string format_str(data, size); | |
29 try { | |
30 #if FMT_FUZZ_FORMAT_TO_STRING | |
31 std::string message = fmt::format(format_str.get(), *value); | |
32 #else | |
33 auto buf = fmt::memory_buffer(); | |
34 fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | |
35 #endif | |
36 } catch (std::exception&) { | |
37 } | |
38 } | |
39 | |
40 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
41 if (size <= 3) return 0; | |
42 | |
43 const auto first = data[0]; | |
44 data++; | |
45 size--; | |
46 | |
47 switch (first) { | |
48 case 0: | |
49 invoke_fmt<bool>(data, size); | |
50 break; | |
51 case 1: | |
52 invoke_fmt<char>(data, size); | |
53 break; | |
54 case 2: | |
55 invoke_fmt<unsigned char>(data, size); | |
56 break; | |
57 case 3: | |
58 invoke_fmt<signed char>(data, size); | |
59 break; | |
60 case 4: | |
61 invoke_fmt<short>(data, size); | |
62 break; | |
63 case 5: | |
64 invoke_fmt<unsigned short>(data, size); | |
65 break; | |
66 case 6: | |
67 invoke_fmt<int>(data, size); | |
68 break; | |
69 case 7: | |
70 invoke_fmt<unsigned int>(data, size); | |
71 break; | |
72 case 8: | |
73 invoke_fmt<long>(data, size); | |
74 break; | |
75 case 9: | |
76 invoke_fmt<unsigned long>(data, size); | |
77 break; | |
78 case 10: | |
79 invoke_fmt<float>(data, size); | |
80 break; | |
81 case 11: | |
82 invoke_fmt<double>(data, size); | |
83 break; | |
84 case 12: | |
85 invoke_fmt<long double>(data, size); | |
86 break; | |
87 case 13: | |
88 invoke_fmt<std::tm, std::time_t>(data, size); | |
89 break; | |
90 } | |
91 return 0; | |
92 } |