Coverage Report

Created: 2026-06-15 06:47

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