Coverage Report

Created: 2025-11-11 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glaze/fuzzing/json_roundtrip_floating.cpp
Line
Count
Source
1
#include <cassert>
2
#include <cmath>
3
#include <cstddef>
4
#include <cstdint>
5
#include <cstring>
6
#include <glaze/glaze.hpp>
7
#include <vector>
8
9
// must be outside test() to work in gcc<14
10
template <typename T>
11
struct Value
12
{
13
   T value{};
14
};
15
16
template <typename T>
17
void test(const uint8_t* Data, size_t Size)
18
1.48k
{
19
1.48k
   using S = Value<T>;
20
1.48k
   S s{};
21
22
1.48k
   if (Size >= sizeof(T)) {
23
1.47k
      std::memcpy(&s.value, Data, sizeof(T));
24
1.47k
   }
25
4
   else {
26
4
      return;
27
4
   }
28
1.47k
   if (std::isfinite(s.value)) {
29
1.47k
      auto str = glz::write_json(s).value_or(std::string{});
30
1.47k
      auto restored = glz::read_json<S>(str);
31
1.47k
      assert(restored);
32
1.47k
      assert(restored.value().value == s.value);
33
1.47k
   }
34
1.47k
}
void test<float>(unsigned char const*, unsigned long)
Line
Count
Source
18
438
{
19
438
   using S = Value<T>;
20
438
   S s{};
21
22
438
   if (Size >= sizeof(T)) {
23
438
      std::memcpy(&s.value, Data, sizeof(T));
24
438
   }
25
0
   else {
26
0
      return;
27
0
   }
28
438
   if (std::isfinite(s.value)) {
29
437
      auto str = glz::write_json(s).value_or(std::string{});
30
437
      auto restored = glz::read_json<S>(str);
31
437
      assert(restored);
32
437
      assert(restored.value().value == s.value);
33
437
   }
34
438
}
void test<double>(unsigned char const*, unsigned long)
Line
Count
Source
18
1.04k
{
19
1.04k
   using S = Value<T>;
20
1.04k
   S s{};
21
22
1.04k
   if (Size >= sizeof(T)) {
23
1.03k
      std::memcpy(&s.value, Data, sizeof(T));
24
1.03k
   }
25
4
   else {
26
4
      return;
27
4
   }
28
1.03k
   if (std::isfinite(s.value)) {
29
1.03k
      auto str = glz::write_json(s).value_or(std::string{});
30
1.03k
      auto restored = glz::read_json<S>(str);
31
1.03k
      assert(restored);
32
1.03k
      assert(restored.value().value == s.value);
33
1.03k
   }
34
1.03k
}
35
36
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
37
1.48k
{
38
1.48k
   if (Size < 1 + sizeof(float)) {
39
4
      return 0;
40
4
   }
41
1.48k
   const auto action = Data[0];
42
1.48k
   ++Data;
43
1.48k
   --Size;
44
45
1.48k
   switch (action & 0b1) {
46
438
   case 0:
47
438
      test<float>(Data, Size);
48
438
      break;
49
1.04k
   case 1:
50
1.04k
      test<double>(Data, Size);
51
1.04k
      break;
52
1.48k
   }
53
54
1.48k
   return 0;
55
1.48k
}