/src/boost/libs/json/fuzzing/fuzz_basic_parser.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) |
3 | | // Copyright (c) 2020 Paul Dreik (github@pauldreik.se) |
4 | | // |
5 | | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
6 | | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
7 | | // |
8 | | // Official repository: https://github.com/boostorg/json |
9 | | // |
10 | | |
11 | | // This file must be manually included when |
12 | | // using basic_parser to implement a parser. |
13 | | #include <boost/json/basic_parser_impl.hpp> |
14 | | |
15 | | #include <iomanip> |
16 | | #include <iostream> |
17 | | |
18 | | using namespace boost::json; |
19 | | |
20 | | class null_parser |
21 | | { |
22 | | struct handler |
23 | | { |
24 | | constexpr static std::size_t max_object_size = std::size_t(-1); |
25 | | constexpr static std::size_t max_array_size = std::size_t(-1); |
26 | | constexpr static std::size_t max_key_size = std::size_t(-1); |
27 | | constexpr static std::size_t max_string_size = std::size_t(-1); |
28 | | |
29 | 5.67k | bool on_document_begin( boost::system::error_code& ) { return true; } |
30 | 1.96k | bool on_document_end( boost::system::error_code& ) { return true; } |
31 | 1.89k | bool on_object_begin( boost::system::error_code& ) { return true; } |
32 | 672 | bool on_object_end( std::size_t, boost::system::error_code& ) { return true; } |
33 | 3.41k | bool on_array_begin( boost::system::error_code& ) { return true; } |
34 | 1.29k | bool on_array_end( std::size_t, boost::system::error_code& ) { return true; } |
35 | 2.09k | bool on_key_part( string_view, std::size_t, boost::system::error_code& ) { return true; } |
36 | 2.15k | bool on_key( string_view, std::size_t, boost::system::error_code& ) { return true; } |
37 | 7.40k | bool on_string_part( string_view, std::size_t, boost::system::error_code& ) { return true; } |
38 | 590 | bool on_string( string_view, std::size_t, boost::system::error_code& ) { return true; } |
39 | 100 | bool on_number_part( string_view, boost::system::error_code& ) { return true; } |
40 | 10.1k | bool on_int64( std::int64_t, string_view, boost::system::error_code& ) { return true; } |
41 | 298 | bool on_uint64( std::uint64_t, string_view, boost::system::error_code& ) { return true; } |
42 | 20.1k | bool on_double( double, string_view, boost::system::error_code& ) { return true; } |
43 | 533 | bool on_bool( bool, boost::system::error_code& ) { return true; } |
44 | 474 | bool on_null( boost::system::error_code& ) { return true; } |
45 | 0 | bool on_comment_part(string_view, boost::system::error_code&) { return true; } |
46 | 0 | bool on_comment(string_view, boost::system::error_code&) { return true; } |
47 | | }; |
48 | | |
49 | | basic_parser<handler> p_; |
50 | | |
51 | | public: |
52 | | null_parser() |
53 | 5.67k | : p_(parse_options()) |
54 | 5.67k | { |
55 | 5.67k | } |
56 | | |
57 | | ~null_parser() |
58 | 5.67k | { |
59 | 5.67k | } |
60 | | |
61 | | std::size_t |
62 | | write( |
63 | | char const* data, |
64 | | std::size_t size, |
65 | | boost::system::error_code& ec) |
66 | 5.67k | { |
67 | 5.67k | auto const n = p_.write_some( false, data, size, ec ); |
68 | 5.67k | if(! ec && n < size) |
69 | 489 | ec = error::extra_data; |
70 | 5.67k | return n; |
71 | 5.67k | } |
72 | | }; |
73 | | |
74 | | bool |
75 | | validate( string_view s ) |
76 | 5.67k | { |
77 | | // Parse with the null parser and return false on error |
78 | 5.67k | null_parser p; |
79 | 5.67k | boost::system::error_code ec; |
80 | 5.67k | p.write( s.data(), s.size(), ec ); |
81 | 5.67k | if( ec ) |
82 | 4.19k | return false; |
83 | | |
84 | | // The string is valid JSON. |
85 | 1.47k | return true; |
86 | 5.67k | } |
87 | | |
88 | | extern "C" |
89 | | int |
90 | | LLVMFuzzerTestOneInput( |
91 | | const uint8_t* data, size_t size) |
92 | 23.0k | { |
93 | 23.0k | try |
94 | 23.0k | { |
95 | 23.0k | validate(string_view{ |
96 | 23.0k | reinterpret_cast<const char* |
97 | 23.0k | >(data), size}); |
98 | 23.0k | } |
99 | 23.0k | catch(...) |
100 | 23.0k | { |
101 | 0 | } |
102 | 23.0k | return 0; |
103 | 23.0k | } |