Coverage Report

Created: 2025-06-24 06:38

/src/boost/libs/json/fuzzing/fuzz_direct_parse.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2024 Mikhail Khachayants (mkhachaiants@gmail.com)
2
//
3
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
//
6
// Official repository: https://github.com/boostorg/json
7
//
8
9
#include <boost/json.hpp>
10
11
#if !defined(BOOST_DESCRIBE_CXX14)
12
13
#include <boost/config/pragma_message.hpp>
14
15
BOOST_PRAGMA_MESSAGE( "This example requires C++14" )
16
17
int main() {}
18
19
#else
20
21
#include <boost/json/parse_into.hpp>
22
#include <boost/variant2/variant.hpp>
23
#include <boost/describe.hpp>
24
#include <map>
25
26
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
27
# include <optional>
28
# define IF_CXX17_HDR_OPTIONAL(...) __VA_ARGS__
29
#else
30
# define IF_CXX17_HDR_OPTIONAL(...)
31
#endif // BOOST_NO_CXX17_HDR_OPTIONAL
32
33
using namespace boost::json;
34
35
struct Object
36
{
37
    bool b;
38
    float f;
39
    double d;
40
    std::int64_t i64;
41
    std::uint64_t u64;
42
    std::string s;
43
    std::vector<bool> v1;
44
    std::vector<std::int64_t> v2;
45
    std::vector<std::uint64_t> v3;
46
    std::array<bool, 3> a1;
47
    std::array<std::int64_t, 3> a2;
48
    std::array<std::uint64_t, 3> a3;
49
    std::map<std::string, std::int64_t> m1;
50
    std::map<std::string, std::string> m2;
51
    std::map<std::string, double> m3;
52
    std::tuple<bool, std::uint64_t, std::int64_t, double, std::string> t1;
53
    std::tuple<std::array<std::string, 3>, std::array<double, 3>, std::nullptr_t> t2;
54
    std::tuple<std::vector<std::string>, std::vector<double>> t3;
55
    boost::variant2::variant<bool, std::uint64_t, std::int64_t, double, std::string> v;
56
57
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
58
    std::optional<bool> ob;
59
    std::optional<std::int64_t> oi;
60
    std::optional<std::uint64_t> ou;
61
    std::optional<double> od;
62
    std::optional<std::string> os;
63
#endif // BOOST_NO_CXX17_HDR_OPTIONAL
64
};
65
66
BOOST_DESCRIBE_STRUCT(Object, (),
67
    (b, i64, u64, f, d, s, v1, v2, v3, a1, a2, a3, m1, m2, m3, t1, t2, t3, v,
68
    IF_CXX17_HDR_OPTIONAL(ob, oi, ou, od, os)))
69
70
71
bool
72
fuzz_direct_parse(string_view sv)
73
10.4k
{
74
10.4k
    Object object;
75
10.4k
    boost::system::error_code ec;
76
10.4k
    parse_into(object, sv, ec);
77
10.4k
    return !ec;
78
10.4k
}
79
80
extern "C"
81
int
82
LLVMFuzzerTestOneInput(
83
        const uint8_t* data, size_t size)
84
23.0k
{
85
23.0k
    try
86
23.0k
    {
87
23.0k
        string_view sv{reinterpret_cast<
88
23.0k
            const char*>(data), size};
89
23.0k
        fuzz_direct_parse(sv);
90
23.0k
    }
91
23.0k
    catch(...)
92
23.0k
    {
93
0
    }
94
23.0k
    return 0;
95
23.0k
}
96
97
#endif // !defined(BOOST_DESCRIBE_CXX14)