Coverage Report

Created: 2025-07-12 06:43

/src/glaze/fuzzing/json_roundtrip_int.cpp
Line
Count
Source (jump to first uncovered line)
1
#include <cassert>
2
#include <cstddef>
3
#include <cstdint>
4
#include <cstring>
5
#include <glaze/glaze.hpp>
6
#include <vector>
7
8
// must be outside test() to work in gcc<14
9
template <typename T>
10
struct Value
11
{
12
   T value{};
13
};
14
15
template <typename T>
16
void test(const uint8_t* Data, size_t Size)
17
1.73k
{
18
1.73k
   using S = Value<T>;
19
1.73k
   S s{};
20
21
1.73k
   if (Size >= sizeof(T)) {
22
1.71k
      std::memcpy(&s.value, Data, sizeof(T));
23
1.71k
   }
24
22
   else {
25
22
      return;
26
22
   }
27
1.71k
   auto str = glz::write_json(s).value_or(std::string{});
28
1.71k
   auto restored = glz::read_json<S>(str);
29
1.71k
   assert(restored);
30
1.71k
   assert(restored.value().value == s.value);
31
1.71k
}
void test<short>(unsigned char const*, unsigned long)
Line
Count
Source
17
106
{
18
106
   using S = Value<T>;
19
106
   S s{};
20
21
106
   if (Size >= sizeof(T)) {
22
105
      std::memcpy(&s.value, Data, sizeof(T));
23
105
   }
24
1
   else {
25
1
      return;
26
1
   }
27
105
   auto str = glz::write_json(s).value_or(std::string{});
28
105
   auto restored = glz::read_json<S>(str);
29
105
   assert(restored);
30
105
   assert(restored.value().value == s.value);
31
105
}
void test<unsigned short>(unsigned char const*, unsigned long)
Line
Count
Source
17
106
{
18
106
   using S = Value<T>;
19
106
   S s{};
20
21
106
   if (Size >= sizeof(T)) {
22
105
      std::memcpy(&s.value, Data, sizeof(T));
23
105
   }
24
1
   else {
25
1
      return;
26
1
   }
27
105
   auto str = glz::write_json(s).value_or(std::string{});
28
105
   auto restored = glz::read_json<S>(str);
29
105
   assert(restored);
30
105
   assert(restored.value().value == s.value);
31
105
}
void test<int>(unsigned char const*, unsigned long)
Line
Count
Source
17
169
{
18
169
   using S = Value<T>;
19
169
   S s{};
20
21
169
   if (Size >= sizeof(T)) {
22
167
      std::memcpy(&s.value, Data, sizeof(T));
23
167
   }
24
2
   else {
25
2
      return;
26
2
   }
27
167
   auto str = glz::write_json(s).value_or(std::string{});
28
167
   auto restored = glz::read_json<S>(str);
29
167
   assert(restored);
30
167
   assert(restored.value().value == s.value);
31
167
}
void test<unsigned int>(unsigned char const*, unsigned long)
Line
Count
Source
17
169
{
18
169
   using S = Value<T>;
19
169
   S s{};
20
21
169
   if (Size >= sizeof(T)) {
22
167
      std::memcpy(&s.value, Data, sizeof(T));
23
167
   }
24
2
   else {
25
2
      return;
26
2
   }
27
167
   auto str = glz::write_json(s).value_or(std::string{});
28
167
   auto restored = glz::read_json<S>(str);
29
167
   assert(restored);
30
167
   assert(restored.value().value == s.value);
31
167
}
void test<long>(unsigned char const*, unsigned long)
Line
Count
Source
17
296
{
18
296
   using S = Value<T>;
19
296
   S s{};
20
21
296
   if (Size >= sizeof(T)) {
22
293
      std::memcpy(&s.value, Data, sizeof(T));
23
293
   }
24
3
   else {
25
3
      return;
26
3
   }
27
293
   auto str = glz::write_json(s).value_or(std::string{});
28
293
   auto restored = glz::read_json<S>(str);
29
293
   assert(restored);
30
293
   assert(restored.value().value == s.value);
31
293
}
void test<unsigned long>(unsigned char const*, unsigned long)
Line
Count
Source
17
296
{
18
296
   using S = Value<T>;
19
296
   S s{};
20
21
296
   if (Size >= sizeof(T)) {
22
293
      std::memcpy(&s.value, Data, sizeof(T));
23
293
   }
24
3
   else {
25
3
      return;
26
3
   }
27
293
   auto str = glz::write_json(s).value_or(std::string{});
28
293
   auto restored = glz::read_json<S>(str);
29
293
   assert(restored);
30
293
   assert(restored.value().value == s.value);
31
293
}
void test<long long>(unsigned char const*, unsigned long)
Line
Count
Source
17
298
{
18
298
   using S = Value<T>;
19
298
   S s{};
20
21
298
   if (Size >= sizeof(T)) {
22
293
      std::memcpy(&s.value, Data, sizeof(T));
23
293
   }
24
5
   else {
25
5
      return;
26
5
   }
27
293
   auto str = glz::write_json(s).value_or(std::string{});
28
293
   auto restored = glz::read_json<S>(str);
29
293
   assert(restored);
30
293
   assert(restored.value().value == s.value);
31
293
}
void test<unsigned long long>(unsigned char const*, unsigned long)
Line
Count
Source
17
298
{
18
298
   using S = Value<T>;
19
298
   S s{};
20
21
298
   if (Size >= sizeof(T)) {
22
293
      std::memcpy(&s.value, Data, sizeof(T));
23
293
   }
24
5
   else {
25
5
      return;
26
5
   }
27
293
   auto str = glz::write_json(s).value_or(std::string{});
28
293
   auto restored = glz::read_json<S>(str);
29
293
   assert(restored);
30
293
   assert(restored.value().value == s.value);
31
293
}
32
33
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
34
870
{
35
870
   if (Size < 2) {
36
1
      return 0;
37
1
   }
38
869
   const auto action = Data[0];
39
869
   ++Data;
40
869
   --Size;
41
42
869
   switch (action & 0b11) {
43
106
   case 0:
44
106
      test<short>(Data, Size);
45
106
      test<unsigned short>(Data, Size);
46
106
      break;
47
169
   case 1:
48
169
      test<int>(Data, Size);
49
169
      test<unsigned int>(Data, Size);
50
169
      break;
51
296
   case 2:
52
296
      test<long>(Data, Size);
53
296
      test<unsigned long>(Data, Size);
54
296
      break;
55
298
   case 3:
56
298
      test<long long>(Data, Size);
57
298
      test<unsigned long long>(Data, Size);
58
298
      break;
59
869
   }
60
61
869
   return 0;
62
869
}