Coverage Report

Created: 2025-01-26 06:54

/src/boost_programoptions_fuzzer.cc
Line
Count
Source
1
/* Copyright 2024 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
// The ideal place for this fuzz target is the boost repository.
13
#include <boost/program_options.hpp>
14
namespace po = boost::program_options;
15
16
#include <sstream>
17
#include <fuzzer/FuzzedDataProvider.h>
18
19
using namespace std;
20
21
po::options_description set_options()
22
2.11k
{
23
2.11k
    po::options_description opts;
24
2.11k
    opts.add_options()
25
2.11k
        ("global_string", po::value<string>())
26
27
2.11k
        ("strings.word", po::value<string>())
28
2.11k
        ("strings.phrase", po::value<string>())
29
2.11k
        ("strings.quoted", po::value<string>())
30
31
2.11k
        ("ints.positive", po::value<int>())
32
2.11k
        ("ints.negative", po::value<int>())
33
2.11k
        ("ints.hex", po::value<int>())
34
2.11k
        ("ints.oct", po::value<int>())
35
2.11k
        ("ints.bin", po::value<int>())
36
37
2.11k
        ("floats.positive", po::value<float>())
38
2.11k
        ("floats.negative", po::value<float>())
39
2.11k
        ("floats.double", po::value<double>())
40
2.11k
        ("floats.int", po::value<float>())
41
2.11k
        ("floats.int_dot", po::value<float>())
42
2.11k
        ("floats.dot", po::value<float>())
43
2.11k
        ("floats.exp_lower", po::value<float>())
44
2.11k
        ("floats.exp_upper", po::value<float>())
45
2.11k
        ("floats.exp_decimal", po::value<float>())
46
2.11k
        ("floats.exp_negative", po::value<float>())
47
2.11k
        ("floats.exp_negative_val", po::value<float>())
48
2.11k
        ("floats.exp_negative_negative_val", po::value<float>())
49
50
2.11k
        ("booleans.number_true", po::bool_switch())
51
2.11k
        ("booleans.number_false", po::bool_switch())
52
2.11k
        ("booleans.yn_true", po::bool_switch())
53
2.11k
        ("booleans.yn_false", po::bool_switch())
54
2.11k
        ("booleans.tf_true", po::bool_switch())
55
2.11k
        ("booleans.tf_false", po::bool_switch())
56
2.11k
        ("booleans.onoff_true", po::bool_switch())
57
2.11k
        ("booleans.onoff_false", po::bool_switch())
58
2.11k
        ("booleans.present_equal_true", po::bool_switch())
59
2.11k
       ("booleans.present_no_equal_true", po::bool_switch())
60
2.11k
       ;
61
2.11k
    return opts;
62
2.11k
}
63
64
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65
16.0k
{
66
16.0k
    try {
67
16.0k
        FuzzedDataProvider fdp(data, size);
68
16.0k
        auto opts = set_options();
69
16.0k
        po::variables_map vars;
70
16.0k
        stringstream st(fdp.ConsumeRemainingBytesAsString());
71
72
16.0k
        const bool ALLOW_UNREGISTERED = true;
73
74
16.0k
        po::parsed_options parsed = parse_config_file(st, opts, ALLOW_UNREGISTERED);
75
16.0k
        store(parsed, vars);
76
16.0k
        vector<string> unregistered = po::collect_unrecognized(parsed.options, po::exclude_positional);
77
16.0k
        notify(vars);
78
16.0k
    } catch(...) {
79
10.6k
    }
80
16.0k
    return 0;
81
16.0k
}