Coverage Report

Created: 2026-06-11 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fmt/test/fuzzing/fuzzer-common.h
Line
Count
Source
1
// Copyright (c) 2019, Paul Dreik
2
// For the license information refer to format.h.
3
4
#ifndef FUZZER_COMMON_H
5
#define FUZZER_COMMON_H
6
7
#include <fmt/base.h>
8
9
#include <cstdint>  // std::uint8_t
10
#include <cstring>  // memcpy
11
#include <vector>
12
13
// One can format to either a string, or a buffer. The latter is faster, but
14
// one may be interested in formatting to a string instead to verify it works
15
// as intended. To avoid a combinatoric explosion, select this at compile time
16
// instead of dynamically from the fuzz data.
17
#define FMT_FUZZ_FORMAT_TO_STRING 0
18
19
// If {fmt} is given a buffer that is separately allocated, chances that address
20
// sanitizer detects out of bound reads is much higher. However, it slows down
21
// the fuzzing.
22
#define FMT_FUZZ_SEPARATE_ALLOCATION 1
23
24
// The size of the largest possible type in use.
25
// To let the fuzzer mutation be efficient at cross pollinating between
26
// different types, use a fixed size format. The same bit pattern, interpreted
27
// as another type, is likely interesting.
28
constexpr auto fixed_size = 16;
29
30
// Casts data to a char pointer.
31
45.0k
template <typename T> inline const char* as_chars(const T* data) {
32
45.0k
  return reinterpret_cast<const char*>(data);
33
45.0k
}
34
35
// Casts data to a byte pointer.
36
template <typename T> inline const std::uint8_t* as_bytes(const T* data) {
37
  return reinterpret_cast<const std::uint8_t*>(data);
38
}
39
40
// Blits bytes from data to form an (assumed trivially constructible) object
41
// of type Item.
42
45.0k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
45.0k
  auto item = Item();
44
45.0k
  std::memcpy(&item, data, sizeof(Item));
45
45.0k
  return item;
46
45.0k
}
char assign_from_buf<char>(unsigned char const*)
Line
Count
Source
42
3.08k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
3.08k
  auto item = Item();
44
3.08k
  std::memcpy(&item, data, sizeof(Item));
45
3.08k
  return item;
46
3.08k
}
signed char assign_from_buf<signed char>(unsigned char const*)
Line
Count
Source
42
3.18k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
3.18k
  auto item = Item();
44
3.18k
  std::memcpy(&item, data, sizeof(Item));
45
3.18k
  return item;
46
3.18k
}
unsigned char assign_from_buf<unsigned char>(unsigned char const*)
Line
Count
Source
42
3.03k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
3.03k
  auto item = Item();
44
3.03k
  std::memcpy(&item, data, sizeof(Item));
45
3.03k
  return item;
46
3.03k
}
short assign_from_buf<short>(unsigned char const*)
Line
Count
Source
42
3.24k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
3.24k
  auto item = Item();
44
3.24k
  std::memcpy(&item, data, sizeof(Item));
45
3.24k
  return item;
46
3.24k
}
unsigned short assign_from_buf<unsigned short>(unsigned char const*)
Line
Count
Source
42
3.01k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
3.01k
  auto item = Item();
44
3.01k
  std::memcpy(&item, data, sizeof(Item));
45
3.01k
  return item;
46
3.01k
}
int assign_from_buf<int>(unsigned char const*)
Line
Count
Source
42
3.43k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
3.43k
  auto item = Item();
44
3.43k
  std::memcpy(&item, data, sizeof(Item));
45
3.43k
  return item;
46
3.43k
}
unsigned int assign_from_buf<unsigned int>(unsigned char const*)
Line
Count
Source
42
3.13k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
3.13k
  auto item = Item();
44
3.13k
  std::memcpy(&item, data, sizeof(Item));
45
3.13k
  return item;
46
3.13k
}
long assign_from_buf<long>(unsigned char const*)
Line
Count
Source
42
4.17k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
4.17k
  auto item = Item();
44
4.17k
  std::memcpy(&item, data, sizeof(Item));
45
4.17k
  return item;
46
4.17k
}
unsigned long assign_from_buf<unsigned long>(unsigned char const*)
Line
Count
Source
42
3.59k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
3.59k
  auto item = Item();
44
3.59k
  std::memcpy(&item, data, sizeof(Item));
45
3.59k
  return item;
46
3.59k
}
float assign_from_buf<float>(unsigned char const*)
Line
Count
Source
42
5.03k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
5.03k
  auto item = Item();
44
5.03k
  std::memcpy(&item, data, sizeof(Item));
45
5.03k
  return item;
46
5.03k
}
double assign_from_buf<double>(unsigned char const*)
Line
Count
Source
42
5.02k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
5.02k
  auto item = Item();
44
5.02k
  std::memcpy(&item, data, sizeof(Item));
45
5.02k
  return item;
46
5.02k
}
long double assign_from_buf<long double>(unsigned char const*)
Line
Count
Source
42
5.13k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
5.13k
  auto item = Item();
44
5.13k
  std::memcpy(&item, data, sizeof(Item));
45
5.13k
  return item;
46
5.13k
}
47
48
// Reads a boolean value by looking at the first byte from data.
49
0
template <> inline bool assign_from_buf<bool>(const std::uint8_t* data) {
50
0
  return *data != 0;
51
0
}
52
53
struct data_to_string {
54
#if FMT_FUZZ_SEPARATE_ALLOCATION
55
  std::vector<char> buffer;
56
57
  data_to_string(const uint8_t* data, size_t size, bool add_terminator = false)
58
0
      : buffer(size + (add_terminator ? 1 : 0)) {
59
0
    if (size) {
60
0
      std::memcpy(buffer.data(), data, size);
61
0
    }
62
0
  }
63
64
0
  fmt::string_view get() const { return {buffer.data(), buffer.size()}; }
65
#else
66
  fmt::string_view sv;
67
68
  data_to_string(const uint8_t* data, size_t size, bool = false)
69
      : str(as_chars(data), size) {}
70
71
  fmt::string_view get() const { return sv; }
72
#endif
73
74
0
  const char* data() const { return get().data(); }
75
};
76
77
#endif  // FUZZER_COMMON_H