File: fuzz_direct_parse.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (97 lines) | stat: -rw-r--r-- 2,414 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2024 Mikhail Khachayants (mkhachaiants@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//

#include <boost/json.hpp>

#if !defined(BOOST_DESCRIBE_CXX14)

#include <boost/config/pragma_message.hpp>

BOOST_PRAGMA_MESSAGE( "This example requires C++14" )

int main() {}

#else

#include <boost/json/parse_into.hpp>
#include <boost/variant2/variant.hpp>
#include <boost/describe.hpp>
#include <map>

#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
# include <optional>
# define IF_CXX17_HDR_OPTIONAL(...) __VA_ARGS__
#else
# define IF_CXX17_HDR_OPTIONAL(...)
#endif // BOOST_NO_CXX17_HDR_OPTIONAL

using namespace boost::json;

struct Object
{
    bool b;
    float f;
    double d;
    std::int64_t i64;
    std::uint64_t u64;
    std::string s;
    std::vector<bool> v1;
    std::vector<std::int64_t> v2;
    std::vector<std::uint64_t> v3;
    std::array<bool, 3> a1;
    std::array<std::int64_t, 3> a2;
    std::array<std::uint64_t, 3> a3;
    std::map<std::string, std::int64_t> m1;
    std::map<std::string, std::string> m2;
    std::map<std::string, double> m3;
    std::tuple<bool, std::uint64_t, std::int64_t, double, std::string> t1;
    std::tuple<std::array<std::string, 3>, std::array<double, 3>, std::nullptr_t> t2;
    std::tuple<std::vector<std::string>, std::vector<double>> t3;
    boost::variant2::variant<bool, std::uint64_t, std::int64_t, double, std::string> v;

#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    std::optional<bool> ob;
    std::optional<std::int64_t> oi;
    std::optional<std::uint64_t> ou;
    std::optional<double> od;
    std::optional<std::string> os;
#endif // BOOST_NO_CXX17_HDR_OPTIONAL
};

BOOST_DESCRIBE_STRUCT(Object, (),
    (b, i64, u64, f, d, s, v1, v2, v3, a1, a2, a3, m1, m2, m3, t1, t2, t3, v,
    IF_CXX17_HDR_OPTIONAL(ob, oi, ou, od, os)))


bool
fuzz_direct_parse(string_view sv)
{
    Object object;
    boost::system::error_code ec;
    parse_into(object, sv, ec);
    return !ec;
}

extern "C"
int
LLVMFuzzerTestOneInput(
        const uint8_t* data, size_t size)
{
    try
    {
        string_view sv{reinterpret_cast<
            const char*>(data), size};
        fuzz_direct_parse(sv);
    }
    catch(...)
    {
    }
    return 0;
}

#endif // !defined(BOOST_DESCRIBE_CXX14)