Coverage Report

Created: 2025-06-13 06:26

/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.71k
        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.92k
        bool on_object_begin( boost::system::error_code& ) { return true; }
32
667
        bool on_object_end( std::size_t, boost::system::error_code& ) { return true; }
33
3.33k
        bool on_array_begin( boost::system::error_code& ) { return true; }
34
1.26k
        bool on_array_end( std::size_t, boost::system::error_code& ) { return true; }
35
1.90k
        bool on_key_part( string_view, std::size_t, boost::system::error_code& ) { return true; }
36
1.89k
        bool on_key( string_view, std::size_t, boost::system::error_code& ) { return true; }
37
6.98k
        bool on_string_part( string_view, std::size_t, boost::system::error_code& ) { return true; }
38
589
        bool on_string( string_view, std::size_t, boost::system::error_code& ) { return true; }
39
103
        bool on_number_part( string_view, boost::system::error_code& ) { return true; }
40
10.7k
        bool on_int64( std::int64_t, string_view, boost::system::error_code& ) { return true; }
41
278
        bool on_uint64( std::uint64_t, string_view, boost::system::error_code& ) { return true; }
42
16.7k
        bool on_double( double, string_view, boost::system::error_code& ) { return true; }
43
586
        bool on_bool( bool, boost::system::error_code& ) { return true; }
44
408
        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.71k
        : p_(parse_options())
54
5.71k
    {
55
5.71k
    }
56
57
    ~null_parser()
58
5.71k
    {
59
5.71k
    }
60
61
    std::size_t
62
    write(
63
        char const* data,
64
        std::size_t size,
65
        boost::system::error_code& ec)
66
5.71k
    {
67
5.71k
        auto const n = p_.write_some( false, data, size, ec );
68
5.71k
        if(! ec && n < size)
69
480
            ec = error::extra_data;
70
5.71k
        return n;
71
5.71k
    }
72
};
73
74
bool
75
validate( string_view s )
76
5.71k
{
77
    // Parse with the null parser and return false on error
78
5.71k
    null_parser p;
79
5.71k
    boost::system::error_code ec;
80
5.71k
    p.write( s.data(), s.size(), ec );
81
5.71k
    if( ec )
82
4.22k
        return false;
83
84
    // The string is valid JSON.
85
1.48k
    return true;
86
5.71k
}
87
88
extern "C"
89
int
90
LLVMFuzzerTestOneInput(
91
    const uint8_t* data, size_t size)
92
21.2k
{
93
21.2k
    try
94
21.2k
    {
95
21.2k
        validate(string_view{
96
21.2k
            reinterpret_cast<const char*
97
21.2k
                >(data), size});
98
21.2k
    }
99
21.2k
    catch(...)
100
21.2k
    {
101
0
    }
102
21.2k
    return 0;
103
21.2k
}