Coverage Report

Created: 2023-06-07 06:25

/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.90k
        bool on_document_begin( error_code& ) { return true; }
30
1.77k
        bool on_document_end( error_code& ) { return true; }
31
2.23k
        bool on_object_begin( error_code& ) { return true; }
32
706
        bool on_object_end( std::size_t, error_code& ) { return true; }
33
3.48k
        bool on_array_begin( error_code& ) { return true; }
34
1.32k
        bool on_array_end( std::size_t, error_code& ) { return true; }
35
1.06k
        bool on_key_part( string_view, std::size_t, error_code& ) { return true; }
36
3.22k
        bool on_key( string_view, std::size_t, error_code& ) { return true; }
37
6.26k
        bool on_string_part( string_view, std::size_t, error_code& ) { return true; }
38
1.34k
        bool on_string( string_view, std::size_t, error_code& ) { return true; }
39
122
        bool on_number_part( string_view, error_code& ) { return true; }
40
10.2k
        bool on_int64( std::int64_t, string_view, error_code& ) { return true; }
41
495
        bool on_uint64( std::uint64_t, string_view, error_code& ) { return true; }
42
18.9k
        bool on_double( double, string_view, error_code& ) { return true; }
43
647
        bool on_bool( bool, error_code& ) { return true; }
44
258
        bool on_null( error_code& ) { return true; }
45
0
        bool on_comment_part(string_view, error_code&) { return true; }
46
0
        bool on_comment(string_view, error_code&) { return true; }
47
    };
48
49
    basic_parser<handler> p_;
50
51
public:
52
    null_parser()
53
        : p_(parse_options())
54
5.90k
    {
55
5.90k
    }
56
57
    ~null_parser()
58
5.90k
    {
59
5.90k
    }
60
61
    std::size_t
62
    write(
63
        char const* data,
64
        std::size_t size,
65
        error_code& ec)
66
5.90k
    {
67
5.90k
        auto const n = p_.write_some( false, data, size, ec );
68
5.90k
        if(! ec && n < size)
69
469
            ec = error::extra_data;
70
5.90k
        return n;
71
5.90k
    }
72
};
73
74
bool
75
validate( string_view s )
76
5.90k
{
77
    // Parse with the null parser and return false on error
78
5.90k
    null_parser p;
79
5.90k
    error_code ec;
80
5.90k
    p.write( s.data(), s.size(), ec );
81
5.90k
    if( ec )
82
4.59k
        return false;
83
84
    // The string is valid JSON.
85
1.30k
    return true;
86
5.90k
}
87
88
extern "C"
89
int
90
LLVMFuzzerTestOneInput(
91
    const uint8_t* data, size_t size)
92
12.7k
{
93
12.7k
    try
94
12.7k
    {
95
12.7k
        validate(string_view{
96
12.7k
            reinterpret_cast<const char*
97
12.7k
                >(data), size});
98
12.7k
    }
99
12.7k
    catch(...)
100
12.7k
    {
101
0
    }
102
12.7k
    return 0;
103
12.7k
}