|
| 1 | +// Copyright (c) 2024 Mikhail Khachayants ([email protected]) |
| 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/parse_into.hpp> |
| 10 | +#include <boost/variant2/variant.hpp> |
| 11 | +#include <boost/describe.hpp> |
| 12 | +#include <map> |
| 13 | + |
| 14 | +#ifndef BOOST_NO_CXX17_HDR_OPTIONAL |
| 15 | +# include <optional> |
| 16 | +# define IF_CXX17_HDR_OPTIONAL(...) __VA_ARGS__ |
| 17 | +#else |
| 18 | +# define IF_CXX17_HDR_OPTIONAL(...) |
| 19 | +#endif // BOOST_NO_CXX17_HDR_OPTIONAL |
| 20 | + |
| 21 | +using namespace boost::json; |
| 22 | + |
| 23 | +struct Object |
| 24 | +{ |
| 25 | + bool b; |
| 26 | + float f; |
| 27 | + double d; |
| 28 | + std::int64_t i64; |
| 29 | + std::uint64_t u64; |
| 30 | + std::string s; |
| 31 | + std::vector<bool> v1; |
| 32 | + std::vector<std::int64_t> v2; |
| 33 | + std::vector<std::uint64_t> v3; |
| 34 | + std::array<bool, 3> a1; |
| 35 | + std::array<std::int64_t, 3> a2; |
| 36 | + std::array<std::uint64_t, 3> a3; |
| 37 | + std::map<std::string, std::int64_t> m1; |
| 38 | + std::map<std::string, std::string> m2; |
| 39 | + std::map<std::string, double> m3; |
| 40 | + std::tuple<bool, std::uint64_t, std::int64_t, double, std::string> t1; |
| 41 | + std::tuple<std::array<std::string, 3>, std::array<double, 3>, std::nullptr_t> t2; |
| 42 | + std::tuple<std::vector<std::string>, std::vector<double>> t3; |
| 43 | + boost::variant2::variant<bool, std::uint64_t, std::int64_t, double, std::string> v; |
| 44 | + |
| 45 | +#ifndef BOOST_NO_CXX17_HDR_OPTIONAL |
| 46 | + std::optional<bool> ob; |
| 47 | + std::optional<std::int64_t> oi; |
| 48 | + std::optional<std::uint64_t> ou; |
| 49 | + std::optional<double> od; |
| 50 | + std::optional<std::string> os; |
| 51 | +#endif // BOOST_NO_CXX17_HDR_OPTIONAL |
| 52 | +}; |
| 53 | + |
| 54 | +BOOST_DESCRIBE_STRUCT(Object, (), |
| 55 | + (b, i64, u64, f, d, s, v1, v2, v3, a1, a2, a3, m1, m2, m3, t1, t2, t3, v, |
| 56 | + IF_CXX17_HDR_OPTIONAL(ob, oi, ou, od, os))) |
| 57 | + |
| 58 | + |
| 59 | +bool |
| 60 | +fuzz_direct_parse(string_view sv) |
| 61 | +{ |
| 62 | + Object object; |
| 63 | + boost::system::error_code ec; |
| 64 | + parse_into(object, sv, ec); |
| 65 | + return !ec; |
| 66 | +} |
| 67 | + |
| 68 | +extern "C" |
| 69 | +int |
| 70 | +LLVMFuzzerTestOneInput( |
| 71 | + const uint8_t* data, size_t size) |
| 72 | +{ |
| 73 | + try |
| 74 | + { |
| 75 | + string_view sv{reinterpret_cast< |
| 76 | + const char*>(data), size}; |
| 77 | + fuzz_direct_parse(sv); |
| 78 | + } |
| 79 | + catch(...) |
| 80 | + { |
| 81 | + } |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
0 commit comments