/src/fmt/test/fuzzing/one-arg.cc
Line | Count | Source |
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 | 9.46k | template <typename T, typename Repr> const T* from_repr(const Repr& r) { |
12 | 9.46k | return &r; |
13 | 9.46k | } bool const* from_repr<bool, bool>(bool const&) Line | Count | Source | 11 | 242 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 242 | return &r; | 13 | 242 | } |
char const* from_repr<char, char>(char const&) Line | Count | Source | 11 | 407 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 407 | return &r; | 13 | 407 | } |
unsigned char const* from_repr<unsigned char, unsigned char>(unsigned char const&) Line | Count | Source | 11 | 98 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 98 | return &r; | 13 | 98 | } |
signed char const* from_repr<signed char, signed char>(signed char const&) Line | Count | Source | 11 | 123 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 123 | return &r; | 13 | 123 | } |
short const* from_repr<short, short>(short const&) Line | Count | Source | 11 | 169 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 169 | return &r; | 13 | 169 | } |
unsigned short const* from_repr<unsigned short, unsigned short>(unsigned short const&) Line | Count | Source | 11 | 69 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 69 | return &r; | 13 | 69 | } |
int const* from_repr<int, int>(int const&) Line | Count | Source | 11 | 311 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 311 | return &r; | 13 | 311 | } |
unsigned int const* from_repr<unsigned int, unsigned int>(unsigned int const&) Line | Count | Source | 11 | 349 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 349 | return &r; | 13 | 349 | } |
long const* from_repr<long, long>(long const&) Line | Count | Source | 11 | 896 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 896 | return &r; | 13 | 896 | } |
unsigned long const* from_repr<unsigned long, unsigned long>(unsigned long const&) Line | Count | Source | 11 | 707 | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 707 | return &r; | 13 | 707 | } |
float const* from_repr<float, float>(float const&) Line | Count | Source | 11 | 1.80k | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 1.80k | return &r; | 13 | 1.80k | } |
double const* from_repr<double, double>(double const&) Line | Count | Source | 11 | 2.65k | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 2.65k | return &r; | 13 | 2.65k | } |
long double const* from_repr<long double, long double>(long double const&) Line | Count | Source | 11 | 1.63k | template <typename T, typename Repr> const T* from_repr(const Repr& r) { | 12 | 1.63k | return &r; | 13 | 1.63k | } |
|
14 | | |
15 | 2.31k | template <> const std::tm* from_repr<std::tm>(const std::time_t& t) { |
16 | 2.31k | return std::localtime(&t); |
17 | 2.31k | } |
18 | | |
19 | | template <typename T, typename Repr = T> |
20 | 11.8k | void invoke_fmt(const uint8_t* data, size_t size) { |
21 | 11.8k | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); |
22 | 11.8k | if (size <= fixed_size) return; |
23 | 11.7k | auto repr = assign_from_buf<Repr>(data); |
24 | 11.7k | const T* value = from_repr<T>(repr); |
25 | 11.7k | if (!value) return; |
26 | 11.7k | data += fixed_size; |
27 | 11.7k | size -= fixed_size; |
28 | 11.7k | data_to_string format_str(data, size); |
29 | 11.7k | try { |
30 | | #if FMT_FUZZ_FORMAT_TO_STRING |
31 | | std::string message = fmt::format(format_str.get(), *value); |
32 | | #else |
33 | 11.7k | auto buf = fmt::memory_buffer(); |
34 | 11.7k | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); |
35 | 11.7k | #endif |
36 | 11.7k | } catch (std::exception&) { |
37 | 9.32k | } |
38 | 11.7k | } void invoke_fmt<bool, bool>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 248 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 248 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 248 | if (size <= fixed_size) return; | 23 | 242 | auto repr = assign_from_buf<Repr>(data); | 24 | 242 | const T* value = from_repr<T>(repr); | 25 | 242 | if (!value) return; | 26 | 242 | data += fixed_size; | 27 | 242 | size -= fixed_size; | 28 | 242 | data_to_string format_str(data, size); | 29 | 242 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 242 | auto buf = fmt::memory_buffer(); | 34 | 242 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 242 | #endif | 36 | 242 | } catch (std::exception&) { | 37 | 216 | } | 38 | 242 | } |
void invoke_fmt<char, char>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 413 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 413 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 413 | if (size <= fixed_size) return; | 23 | 407 | auto repr = assign_from_buf<Repr>(data); | 24 | 407 | const T* value = from_repr<T>(repr); | 25 | 407 | if (!value) return; | 26 | 407 | data += fixed_size; | 27 | 407 | size -= fixed_size; | 28 | 407 | data_to_string format_str(data, size); | 29 | 407 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 407 | auto buf = fmt::memory_buffer(); | 34 | 407 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 407 | #endif | 36 | 407 | } catch (std::exception&) { | 37 | 369 | } | 38 | 407 | } |
void invoke_fmt<unsigned char, unsigned char>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 104 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 104 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 104 | if (size <= fixed_size) return; | 23 | 98 | auto repr = assign_from_buf<Repr>(data); | 24 | 98 | const T* value = from_repr<T>(repr); | 25 | 98 | if (!value) return; | 26 | 98 | data += fixed_size; | 27 | 98 | size -= fixed_size; | 28 | 98 | data_to_string format_str(data, size); | 29 | 98 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 98 | auto buf = fmt::memory_buffer(); | 34 | 98 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 98 | #endif | 36 | 98 | } catch (std::exception&) { | 37 | 61 | } | 38 | 98 | } |
void invoke_fmt<signed char, signed char>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 128 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 128 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 128 | if (size <= fixed_size) return; | 23 | 123 | auto repr = assign_from_buf<Repr>(data); | 24 | 123 | const T* value = from_repr<T>(repr); | 25 | 123 | if (!value) return; | 26 | 123 | data += fixed_size; | 27 | 123 | size -= fixed_size; | 28 | 123 | data_to_string format_str(data, size); | 29 | 123 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 123 | auto buf = fmt::memory_buffer(); | 34 | 123 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 123 | #endif | 36 | 123 | } catch (std::exception&) { | 37 | 96 | } | 38 | 123 | } |
void invoke_fmt<short, short>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 175 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 175 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 175 | if (size <= fixed_size) return; | 23 | 169 | auto repr = assign_from_buf<Repr>(data); | 24 | 169 | const T* value = from_repr<T>(repr); | 25 | 169 | if (!value) return; | 26 | 169 | data += fixed_size; | 27 | 169 | size -= fixed_size; | 28 | 169 | data_to_string format_str(data, size); | 29 | 169 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 169 | auto buf = fmt::memory_buffer(); | 34 | 169 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 169 | #endif | 36 | 169 | } catch (std::exception&) { | 37 | 140 | } | 38 | 169 | } |
void invoke_fmt<unsigned short, unsigned short>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 75 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 75 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 75 | if (size <= fixed_size) return; | 23 | 69 | auto repr = assign_from_buf<Repr>(data); | 24 | 69 | const T* value = from_repr<T>(repr); | 25 | 69 | if (!value) return; | 26 | 69 | data += fixed_size; | 27 | 69 | size -= fixed_size; | 28 | 69 | data_to_string format_str(data, size); | 29 | 69 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 69 | auto buf = fmt::memory_buffer(); | 34 | 69 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 69 | #endif | 36 | 69 | } catch (std::exception&) { | 37 | 52 | } | 38 | 69 | } |
void invoke_fmt<int, int>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 317 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 317 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 317 | if (size <= fixed_size) return; | 23 | 311 | auto repr = assign_from_buf<Repr>(data); | 24 | 311 | const T* value = from_repr<T>(repr); | 25 | 311 | if (!value) return; | 26 | 311 | data += fixed_size; | 27 | 311 | size -= fixed_size; | 28 | 311 | data_to_string format_str(data, size); | 29 | 311 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 311 | auto buf = fmt::memory_buffer(); | 34 | 311 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 311 | #endif | 36 | 311 | } catch (std::exception&) { | 37 | 238 | } | 38 | 311 | } |
void invoke_fmt<unsigned int, unsigned int>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 355 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 355 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 355 | if (size <= fixed_size) return; | 23 | 349 | auto repr = assign_from_buf<Repr>(data); | 24 | 349 | const T* value = from_repr<T>(repr); | 25 | 349 | if (!value) return; | 26 | 349 | data += fixed_size; | 27 | 349 | size -= fixed_size; | 28 | 349 | data_to_string format_str(data, size); | 29 | 349 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 349 | auto buf = fmt::memory_buffer(); | 34 | 349 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 349 | #endif | 36 | 349 | } catch (std::exception&) { | 37 | 245 | } | 38 | 349 | } |
void invoke_fmt<long, long>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 899 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 899 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 899 | if (size <= fixed_size) return; | 23 | 896 | auto repr = assign_from_buf<Repr>(data); | 24 | 896 | const T* value = from_repr<T>(repr); | 25 | 896 | if (!value) return; | 26 | 896 | data += fixed_size; | 27 | 896 | size -= fixed_size; | 28 | 896 | data_to_string format_str(data, size); | 29 | 896 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 896 | auto buf = fmt::memory_buffer(); | 34 | 896 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 896 | #endif | 36 | 896 | } catch (std::exception&) { | 37 | 700 | } | 38 | 896 | } |
void invoke_fmt<unsigned long, unsigned long>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 709 | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 709 | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 709 | if (size <= fixed_size) return; | 23 | 707 | auto repr = assign_from_buf<Repr>(data); | 24 | 707 | const T* value = from_repr<T>(repr); | 25 | 707 | if (!value) return; | 26 | 707 | data += fixed_size; | 27 | 707 | size -= fixed_size; | 28 | 707 | data_to_string format_str(data, size); | 29 | 707 | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 707 | auto buf = fmt::memory_buffer(); | 34 | 707 | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 707 | #endif | 36 | 707 | } catch (std::exception&) { | 37 | 509 | } | 38 | 707 | } |
void invoke_fmt<float, float>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 1.81k | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 1.81k | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 1.81k | if (size <= fixed_size) return; | 23 | 1.80k | auto repr = assign_from_buf<Repr>(data); | 24 | 1.80k | const T* value = from_repr<T>(repr); | 25 | 1.80k | if (!value) return; | 26 | 1.80k | data += fixed_size; | 27 | 1.80k | size -= fixed_size; | 28 | 1.80k | data_to_string format_str(data, size); | 29 | 1.80k | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 1.80k | auto buf = fmt::memory_buffer(); | 34 | 1.80k | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 1.80k | #endif | 36 | 1.80k | } catch (std::exception&) { | 37 | 1.34k | } | 38 | 1.80k | } |
void invoke_fmt<double, double>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 2.65k | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 2.65k | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 2.65k | if (size <= fixed_size) return; | 23 | 2.65k | auto repr = assign_from_buf<Repr>(data); | 24 | 2.65k | const T* value = from_repr<T>(repr); | 25 | 2.65k | if (!value) return; | 26 | 2.65k | data += fixed_size; | 27 | 2.65k | size -= fixed_size; | 28 | 2.65k | data_to_string format_str(data, size); | 29 | 2.65k | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 2.65k | auto buf = fmt::memory_buffer(); | 34 | 2.65k | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 2.65k | #endif | 36 | 2.65k | } catch (std::exception&) { | 37 | 1.91k | } | 38 | 2.65k | } |
void invoke_fmt<long double, long double>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 1.63k | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 1.63k | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 1.63k | if (size <= fixed_size) return; | 23 | 1.63k | auto repr = assign_from_buf<Repr>(data); | 24 | 1.63k | const T* value = from_repr<T>(repr); | 25 | 1.63k | if (!value) return; | 26 | 1.63k | data += fixed_size; | 27 | 1.63k | size -= fixed_size; | 28 | 1.63k | data_to_string format_str(data, size); | 29 | 1.63k | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 1.63k | auto buf = fmt::memory_buffer(); | 34 | 1.63k | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 1.63k | #endif | 36 | 1.63k | } catch (std::exception&) { | 37 | 1.24k | } | 38 | 1.63k | } |
void invoke_fmt<tm, long>(unsigned char const*, unsigned long) Line | Count | Source | 20 | 2.31k | void invoke_fmt(const uint8_t* data, size_t size) { | 21 | 2.31k | static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small"); | 22 | 2.31k | if (size <= fixed_size) return; | 23 | 2.31k | auto repr = assign_from_buf<Repr>(data); | 24 | 2.31k | const T* value = from_repr<T>(repr); | 25 | 2.31k | if (!value) return; | 26 | 2.30k | data += fixed_size; | 27 | 2.30k | size -= fixed_size; | 28 | 2.30k | data_to_string format_str(data, size); | 29 | 2.30k | try { | 30 | | #if FMT_FUZZ_FORMAT_TO_STRING | 31 | | std::string message = fmt::format(format_str.get(), *value); | 32 | | #else | 33 | 2.30k | auto buf = fmt::memory_buffer(); | 34 | 2.30k | fmt::format_to(std::back_inserter(buf), format_str.get(), *value); | 35 | 2.30k | #endif | 36 | 2.30k | } catch (std::exception&) { | 37 | 2.19k | } | 38 | 2.30k | } |
|
39 | | |
40 | 11.8k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
41 | 11.8k | if (size <= 3) return 0; |
42 | | |
43 | 11.8k | const auto first = data[0]; |
44 | 11.8k | data++; |
45 | 11.8k | size--; |
46 | | |
47 | 11.8k | switch (first) { |
48 | 248 | case 0: |
49 | 248 | invoke_fmt<bool>(data, size); |
50 | 248 | break; |
51 | 413 | case 1: |
52 | 413 | invoke_fmt<char>(data, size); |
53 | 413 | break; |
54 | 104 | case 2: |
55 | 104 | invoke_fmt<unsigned char>(data, size); |
56 | 104 | break; |
57 | 128 | case 3: |
58 | 128 | invoke_fmt<signed char>(data, size); |
59 | 128 | break; |
60 | 175 | case 4: |
61 | 175 | invoke_fmt<short>(data, size); |
62 | 175 | break; |
63 | 75 | case 5: |
64 | 75 | invoke_fmt<unsigned short>(data, size); |
65 | 75 | break; |
66 | 317 | case 6: |
67 | 317 | invoke_fmt<int>(data, size); |
68 | 317 | break; |
69 | 355 | case 7: |
70 | 355 | invoke_fmt<unsigned int>(data, size); |
71 | 355 | break; |
72 | 899 | case 8: |
73 | 899 | invoke_fmt<long>(data, size); |
74 | 899 | break; |
75 | 709 | case 9: |
76 | 709 | invoke_fmt<unsigned long>(data, size); |
77 | 709 | break; |
78 | 1.81k | case 10: |
79 | 1.81k | invoke_fmt<float>(data, size); |
80 | 1.81k | break; |
81 | 2.65k | case 11: |
82 | 2.65k | invoke_fmt<double>(data, size); |
83 | 2.65k | break; |
84 | 1.63k | case 12: |
85 | 1.63k | invoke_fmt<long double>(data, size); |
86 | 1.63k | break; |
87 | 2.31k | case 13: |
88 | 2.31k | invoke_fmt<std::tm, std::time_t>(data, size); |
89 | 2.31k | break; |
90 | 11.8k | } |
91 | 11.8k | return 0; |
92 | 11.8k | } |