Coverage Report

Created: 2023-03-26 06:57

/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.88k
        bool on_document_begin( error_code& ) { return true; }
30
1.78k
        bool on_document_end( error_code& ) { return true; }
31
2.38k
        bool on_object_begin( error_code& ) { return true; }
32
759
        bool on_object_end( std::size_t, error_code& ) { return true; }
33
3.45k
        bool on_array_begin( error_code& ) { return true; }
34
1.28k
        bool on_array_end( std::size_t, error_code& ) { return true; }
35
1.40k
        bool on_key_part( string_view, std::size_t, error_code& ) { return true; }
36
3.46k
        bool on_key( string_view, std::size_t, error_code& ) { return true; }
37
5.89k
        bool on_string_part( string_view, std::size_t, error_code& ) { return true; }
38
1.37k
        bool on_string( string_view, std::size_t, error_code& ) { return true; }
39
113
        bool on_number_part( string_view, error_code& ) { return true; }
40
9.96k
        bool on_int64( std::int64_t, string_view, error_code& ) { return true; }
41
520
        bool on_uint64( std::uint64_t, string_view, error_code& ) { return true; }
42
17.6k
        bool on_double( double, string_view, error_code& ) { return true; }
43
640
        bool on_bool( bool, error_code& ) { return true; }
44
262
        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.88k
    {
55
5.88k
    }
56
57
    ~null_parser()
58
5.88k
    {
59
5.88k
    }
60
61
    std::size_t
62
    write(
63
        char const* data,
64
        std::size_t size,
65
        error_code& ec)
66
5.88k
    {
67
5.88k
        auto const n = p_.write_some( false, data, size, ec );
68
5.88k
        if(! ec && n < size)
69
492
            ec = error::extra_data;
70
5.88k
        return n;
71
5.88k
    }
72
};
73
74
bool
75
validate( string_view s )
76
5.88k
{
77
    // Parse with the null parser and return false on error
78
5.88k
    null_parser p;
79
5.88k
    error_code ec;
80
5.88k
    p.write( s.data(), s.size(), ec );
81
5.88k
    if( ec )
82
4.59k
        return false;
83
84
    // The string is valid JSON.
85
1.28k
    return true;
86
5.88k
}
87
88
extern "C"
89
int
90
LLVMFuzzerTestOneInput(
91
    const uint8_t* data, size_t size)
92
12.5k
{
93
12.5k
    try
94
12.5k
    {
95
12.5k
        validate(string_view{
96
12.5k
            reinterpret_cast<const char*
97
12.5k
                >(data), size});
98
12.5k
    }
99
12.5k
    catch(...)
100
12.5k
    {
101
0
    }
102
12.5k
    return 0;
103
12.5k
}