]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/fuzzing/fuzz_parser.cpp
1 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2 // Copyright (c) 2020 Paul Dreik (github@pauldreik.se)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/json
10 #include <boost/json/parse_options.hpp>
11 #include <boost/json/serialize.hpp>
12 #include <boost/json/stream_parser.hpp>
13 #include <boost/json/monotonic_resource.hpp>
14 #include <boost/json/null_resource.hpp>
15 #include <boost/json/static_resource.hpp>
18 using namespace boost::json
;
23 std::size_t memlimit1
;
24 std::size_t memlimit2
;
26 void run(stream_parser
& p
) {
29 // Write the first part of the buffer
30 p
.write( jsontext
, ec
);
35 // Take ownership of the resulting value.
38 value jv
= p
.release();
39 res
=serialize(jv
).size()==42;
44 // easy case - everything default
46 stream_parser
p(storage_ptr
{}, opt
);
51 monotonic_resource mr
;
52 stream_parser
p(storage_ptr
{}, opt
);
58 void useLocalBuffer() {
59 std::unique_ptr
<unsigned char[]> temp(new unsigned char[memlimit1
]);
69 // this is on the heap because the size is chosen dynamically
70 std::unique_ptr
<unsigned char[]> temp(new unsigned char[memlimit1
]);
71 stream_parser
p(get_null_resource(),
76 // this is on the heap because the size is chosen dynamically
77 std::unique_ptr
<unsigned char[]> buf(new unsigned char[memlimit2
]);
78 static_resource
mr2( buf
.get(), memlimit2
);
89 LLVMFuzzerTestOneInput(
90 const uint8_t* data
, size_t size
)
98 fh
.opt
.allow_comments
=!!(data
[0]&0x1);
99 fh
.opt
.allow_trailing_commas
=!!(data
[0]&0x2);
100 fh
.opt
.allow_invalid_utf8
=!!(data
[0]&0x4);
101 fh
.opt
.max_depth
= (data
[0]>>3);
103 // select memory strategy to use
104 const int strategy
=data
[1] & 0x3;
107 fh
.memlimit1
=data
[2]*256+data
[3];
108 fh
.memlimit2
=data
[4]*256+data
[5];
113 //set the json string to parse
114 fh
.jsontext
=string_view
{
115 reinterpret_cast<const char*>(