Coverage Report

Created: 2025-04-11 06:02

/src/fmt/test/fuzzing/one-arg.cc
Line
Count
Source (jump to first uncovered line)
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
10.1k
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
10.1k
  return &r;
13
10.1k
}
bool const* from_repr<bool, bool>(bool const&)
Line
Count
Source
11
277
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
277
  return &r;
13
277
}
char const* from_repr<char, char>(char const&)
Line
Count
Source
11
455
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
455
  return &r;
13
455
}
unsigned char const* from_repr<unsigned char, unsigned char>(unsigned char const&)
Line
Count
Source
11
90
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
90
  return &r;
13
90
}
signed char const* from_repr<signed char, signed char>(signed char const&)
Line
Count
Source
11
154
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
154
  return &r;
13
154
}
short const* from_repr<short, short>(short const&)
Line
Count
Source
11
149
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
149
  return &r;
13
149
}
unsigned short const* from_repr<unsigned short, unsigned short>(unsigned short const&)
Line
Count
Source
11
79
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
79
  return &r;
13
79
}
int const* from_repr<int, int>(int const&)
Line
Count
Source
11
441
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
441
  return &r;
13
441
}
unsigned int const* from_repr<unsigned int, unsigned int>(unsigned int const&)
Line
Count
Source
11
262
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
262
  return &r;
13
262
}
long const* from_repr<long, long>(long const&)
Line
Count
Source
11
977
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
977
  return &r;
13
977
}
unsigned long const* from_repr<unsigned long, unsigned long>(unsigned long const&)
Line
Count
Source
11
747
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
747
  return &r;
13
747
}
float const* from_repr<float, float>(float const&)
Line
Count
Source
11
1.85k
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
1.85k
  return &r;
13
1.85k
}
double const* from_repr<double, double>(double const&)
Line
Count
Source
11
2.75k
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
2.75k
  return &r;
13
2.75k
}
long double const* from_repr<long double, long double>(long double const&)
Line
Count
Source
11
1.95k
template <typename T, typename Repr> const T* from_repr(const Repr& r) {
12
1.95k
  return &r;
13
1.95k
}
14
15
2.47k
template <> const std::tm* from_repr<std::tm>(const std::time_t& t) {
16
2.47k
  return std::localtime(&t);
17
2.47k
}
18
19
template <typename T, typename Repr = T>
20
12.7k
void invoke_fmt(const uint8_t* data, size_t size) {
21
12.7k
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
12.7k
  if (size <= fixed_size) return;
23
12.6k
  auto repr = assign_from_buf<Repr>(data);
24
12.6k
  const T* value = from_repr<T>(repr);
25
12.6k
  if (!value) return;
26
12.6k
  data += fixed_size;
27
12.6k
  size -= fixed_size;
28
12.6k
  data_to_string format_str(data, size);
29
12.6k
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
12.6k
    auto buf = fmt::memory_buffer();
34
12.6k
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
12.6k
#endif
36
12.6k
  } catch (std::exception&) {
37
10.2k
  }
38
12.6k
}
void invoke_fmt<bool, bool>(unsigned char const*, unsigned long)
Line
Count
Source
20
283
void invoke_fmt(const uint8_t* data, size_t size) {
21
283
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
283
  if (size <= fixed_size) return;
23
277
  auto repr = assign_from_buf<Repr>(data);
24
277
  const T* value = from_repr<T>(repr);
25
277
  if (!value) return;
26
277
  data += fixed_size;
27
277
  size -= fixed_size;
28
277
  data_to_string format_str(data, size);
29
277
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
277
    auto buf = fmt::memory_buffer();
34
277
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
277
#endif
36
277
  } catch (std::exception&) {
37
240
  }
38
277
}
void invoke_fmt<char, char>(unsigned char const*, unsigned long)
Line
Count
Source
20
459
void invoke_fmt(const uint8_t* data, size_t size) {
21
459
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
459
  if (size <= fixed_size) return;
23
455
  auto repr = assign_from_buf<Repr>(data);
24
455
  const T* value = from_repr<T>(repr);
25
455
  if (!value) return;
26
455
  data += fixed_size;
27
455
  size -= fixed_size;
28
455
  data_to_string format_str(data, size);
29
455
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
455
    auto buf = fmt::memory_buffer();
34
455
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
455
#endif
36
455
  } catch (std::exception&) {
37
414
  }
38
455
}
void invoke_fmt<unsigned char, unsigned char>(unsigned char const*, unsigned long)
Line
Count
Source
20
96
void invoke_fmt(const uint8_t* data, size_t size) {
21
96
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
96
  if (size <= fixed_size) return;
23
90
  auto repr = assign_from_buf<Repr>(data);
24
90
  const T* value = from_repr<T>(repr);
25
90
  if (!value) return;
26
90
  data += fixed_size;
27
90
  size -= fixed_size;
28
90
  data_to_string format_str(data, size);
29
90
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
90
    auto buf = fmt::memory_buffer();
34
90
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
90
#endif
36
90
  } catch (std::exception&) {
37
60
  }
38
90
}
void invoke_fmt<signed char, signed char>(unsigned char const*, unsigned long)
Line
Count
Source
20
160
void invoke_fmt(const uint8_t* data, size_t size) {
21
160
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
160
  if (size <= fixed_size) return;
23
154
  auto repr = assign_from_buf<Repr>(data);
24
154
  const T* value = from_repr<T>(repr);
25
154
  if (!value) return;
26
154
  data += fixed_size;
27
154
  size -= fixed_size;
28
154
  data_to_string format_str(data, size);
29
154
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
154
    auto buf = fmt::memory_buffer();
34
154
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
154
#endif
36
154
  } catch (std::exception&) {
37
121
  }
38
154
}
void invoke_fmt<short, short>(unsigned char const*, unsigned long)
Line
Count
Source
20
155
void invoke_fmt(const uint8_t* data, size_t size) {
21
155
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
155
  if (size <= fixed_size) return;
23
149
  auto repr = assign_from_buf<Repr>(data);
24
149
  const T* value = from_repr<T>(repr);
25
149
  if (!value) return;
26
149
  data += fixed_size;
27
149
  size -= fixed_size;
28
149
  data_to_string format_str(data, size);
29
149
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
149
    auto buf = fmt::memory_buffer();
34
149
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
149
#endif
36
149
  } catch (std::exception&) {
37
125
  }
38
149
}
void invoke_fmt<unsigned short, unsigned short>(unsigned char const*, unsigned long)
Line
Count
Source
20
82
void invoke_fmt(const uint8_t* data, size_t size) {
21
82
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
82
  if (size <= fixed_size) return;
23
79
  auto repr = assign_from_buf<Repr>(data);
24
79
  const T* value = from_repr<T>(repr);
25
79
  if (!value) return;
26
79
  data += fixed_size;
27
79
  size -= fixed_size;
28
79
  data_to_string format_str(data, size);
29
79
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
79
    auto buf = fmt::memory_buffer();
34
79
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
79
#endif
36
79
  } catch (std::exception&) {
37
59
  }
38
79
}
void invoke_fmt<int, int>(unsigned char const*, unsigned long)
Line
Count
Source
20
447
void invoke_fmt(const uint8_t* data, size_t size) {
21
447
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
447
  if (size <= fixed_size) return;
23
441
  auto repr = assign_from_buf<Repr>(data);
24
441
  const T* value = from_repr<T>(repr);
25
441
  if (!value) return;
26
441
  data += fixed_size;
27
441
  size -= fixed_size;
28
441
  data_to_string format_str(data, size);
29
441
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
441
    auto buf = fmt::memory_buffer();
34
441
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
441
#endif
36
441
  } catch (std::exception&) {
37
350
  }
38
441
}
void invoke_fmt<unsigned int, unsigned int>(unsigned char const*, unsigned long)
Line
Count
Source
20
268
void invoke_fmt(const uint8_t* data, size_t size) {
21
268
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
268
  if (size <= fixed_size) return;
23
262
  auto repr = assign_from_buf<Repr>(data);
24
262
  const T* value = from_repr<T>(repr);
25
262
  if (!value) return;
26
262
  data += fixed_size;
27
262
  size -= fixed_size;
28
262
  data_to_string format_str(data, size);
29
262
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
262
    auto buf = fmt::memory_buffer();
34
262
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
262
#endif
36
262
  } catch (std::exception&) {
37
184
  }
38
262
}
void invoke_fmt<long, long>(unsigned char const*, unsigned long)
Line
Count
Source
20
983
void invoke_fmt(const uint8_t* data, size_t size) {
21
983
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
983
  if (size <= fixed_size) return;
23
977
  auto repr = assign_from_buf<Repr>(data);
24
977
  const T* value = from_repr<T>(repr);
25
977
  if (!value) return;
26
977
  data += fixed_size;
27
977
  size -= fixed_size;
28
977
  data_to_string format_str(data, size);
29
977
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
977
    auto buf = fmt::memory_buffer();
34
977
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
977
#endif
36
977
  } catch (std::exception&) {
37
740
  }
38
977
}
void invoke_fmt<unsigned long, unsigned long>(unsigned char const*, unsigned long)
Line
Count
Source
20
753
void invoke_fmt(const uint8_t* data, size_t size) {
21
753
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
753
  if (size <= fixed_size) return;
23
747
  auto repr = assign_from_buf<Repr>(data);
24
747
  const T* value = from_repr<T>(repr);
25
747
  if (!value) return;
26
747
  data += fixed_size;
27
747
  size -= fixed_size;
28
747
  data_to_string format_str(data, size);
29
747
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
747
    auto buf = fmt::memory_buffer();
34
747
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
747
#endif
36
747
  } catch (std::exception&) {
37
533
  }
38
747
}
void invoke_fmt<float, float>(unsigned char const*, unsigned long)
Line
Count
Source
20
1.85k
void invoke_fmt(const uint8_t* data, size_t size) {
21
1.85k
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
1.85k
  if (size <= fixed_size) return;
23
1.85k
  auto repr = assign_from_buf<Repr>(data);
24
1.85k
  const T* value = from_repr<T>(repr);
25
1.85k
  if (!value) return;
26
1.85k
  data += fixed_size;
27
1.85k
  size -= fixed_size;
28
1.85k
  data_to_string format_str(data, size);
29
1.85k
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
1.85k
    auto buf = fmt::memory_buffer();
34
1.85k
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
1.85k
#endif
36
1.85k
  } catch (std::exception&) {
37
1.44k
  }
38
1.85k
}
void invoke_fmt<double, double>(unsigned char const*, unsigned long)
Line
Count
Source
20
2.76k
void invoke_fmt(const uint8_t* data, size_t size) {
21
2.76k
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
2.76k
  if (size <= fixed_size) return;
23
2.75k
  auto repr = assign_from_buf<Repr>(data);
24
2.75k
  const T* value = from_repr<T>(repr);
25
2.75k
  if (!value) return;
26
2.75k
  data += fixed_size;
27
2.75k
  size -= fixed_size;
28
2.75k
  data_to_string format_str(data, size);
29
2.75k
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
2.75k
    auto buf = fmt::memory_buffer();
34
2.75k
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
2.75k
#endif
36
2.75k
  } catch (std::exception&) {
37
2.03k
  }
38
2.75k
}
void invoke_fmt<long double, long double>(unsigned char const*, unsigned long)
Line
Count
Source
20
1.95k
void invoke_fmt(const uint8_t* data, size_t size) {
21
1.95k
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
1.95k
  if (size <= fixed_size) return;
23
1.95k
  auto repr = assign_from_buf<Repr>(data);
24
1.95k
  const T* value = from_repr<T>(repr);
25
1.95k
  if (!value) return;
26
1.95k
  data += fixed_size;
27
1.95k
  size -= fixed_size;
28
1.95k
  data_to_string format_str(data, size);
29
1.95k
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
1.95k
    auto buf = fmt::memory_buffer();
34
1.95k
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
1.95k
#endif
36
1.95k
  } catch (std::exception&) {
37
1.50k
  }
38
1.95k
}
void invoke_fmt<tm, long>(unsigned char const*, unsigned long)
Line
Count
Source
20
2.47k
void invoke_fmt(const uint8_t* data, size_t size) {
21
2.47k
  static_assert(sizeof(Repr) <= fixed_size, "Nfixed is too small");
22
2.47k
  if (size <= fixed_size) return;
23
2.47k
  auto repr = assign_from_buf<Repr>(data);
24
2.47k
  const T* value = from_repr<T>(repr);
25
2.47k
  if (!value) return;
26
2.47k
  data += fixed_size;
27
2.47k
  size -= fixed_size;
28
2.47k
  data_to_string format_str(data, size);
29
2.47k
  try {
30
#if FMT_FUZZ_FORMAT_TO_STRING
31
    std::string message = fmt::format(format_str.get(), *value);
32
#else
33
2.47k
    auto buf = fmt::memory_buffer();
34
2.47k
    fmt::format_to(std::back_inserter(buf), format_str.get(), *value);
35
2.47k
#endif
36
2.47k
  } catch (std::exception&) {
37
2.40k
  }
38
2.47k
}
39
40
12.7k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
41
12.7k
  if (size <= 3) return 0;
42
43
12.7k
  const auto first = data[0];
44
12.7k
  data++;
45
12.7k
  size--;
46
47
12.7k
  switch (first) {
48
283
  case 0:
49
283
    invoke_fmt<bool>(data, size);
50
283
    break;
51
459
  case 1:
52
459
    invoke_fmt<char>(data, size);
53
459
    break;
54
96
  case 2:
55
96
    invoke_fmt<unsigned char>(data, size);
56
96
    break;
57
160
  case 3:
58
160
    invoke_fmt<signed char>(data, size);
59
160
    break;
60
155
  case 4:
61
155
    invoke_fmt<short>(data, size);
62
155
    break;
63
82
  case 5:
64
82
    invoke_fmt<unsigned short>(data, size);
65
82
    break;
66
447
  case 6:
67
447
    invoke_fmt<int>(data, size);
68
447
    break;
69
268
  case 7:
70
268
    invoke_fmt<unsigned int>(data, size);
71
268
    break;
72
983
  case 8:
73
983
    invoke_fmt<long>(data, size);
74
983
    break;
75
753
  case 9:
76
753
    invoke_fmt<unsigned long>(data, size);
77
753
    break;
78
1.85k
  case 10:
79
1.85k
    invoke_fmt<float>(data, size);
80
1.85k
    break;
81
2.76k
  case 11:
82
2.76k
    invoke_fmt<double>(data, size);
83
2.76k
    break;
84
1.95k
  case 12:
85
1.95k
    invoke_fmt<long double>(data, size);
86
1.95k
    break;
87
2.47k
  case 13:
88
2.47k
    invoke_fmt<std::tm, std::time_t>(data, size);
89
2.47k
    break;
90
12.7k
  }
91
12.7k
  return 0;
92
12.7k
}