1 // Copyright (c) 2020-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <blockfilter.h>
6 #include <clientversion.h>
7 #include <logging.h>
8 #include <netaddress.h>
9 #include <netbase.h>
10 #include <outputtype.h>
11 #include <rpc/client.h>
12 #include <rpc/request.h>
13 #include <rpc/server.h>
14 #include <rpc/util.h>
15 #include <script/descriptor.h>
16 #include <script/script.h>
17 #include <serialize.h>
18 #include <streams.h>
19 #include <test/fuzz/FuzzedDataProvider.h>
20 #include <test/fuzz/fuzz.h>
21 #include <test/fuzz/util.h>
22 #include <util/error.h>
23 #include <util/fees.h>
24 #include <util/message.h>
25 #include <util/settings.h>
26 #include <util/strencodings.h>
27 #include <util/string.h>
28 #include <util/system.h>
29 #include <util/translation.h>
30 #include <util/url.h>
31 #include <version.h>
32 
33 #include <cstdint>
34 #include <string>
35 #include <vector>
36 
FUZZ_TARGET(string)37 FUZZ_TARGET(string)
38 {
39     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
40     const std::string random_string_1 = fuzzed_data_provider.ConsumeRandomLengthString(32);
41     const std::string random_string_2 = fuzzed_data_provider.ConsumeRandomLengthString(32);
42     const std::vector<std::string> random_string_vector = ConsumeRandomLengthStringVector(fuzzed_data_provider);
43 
44     (void)AmountErrMsg(random_string_1, random_string_2);
45     (void)AmountHighWarn(random_string_1);
46     BlockFilterType block_filter_type;
47     (void)BlockFilterTypeByName(random_string_1, block_filter_type);
48     (void)Capitalize(random_string_1);
49     (void)CopyrightHolders(random_string_1);
50     FeeEstimateMode fee_estimate_mode;
51     (void)FeeModeFromString(random_string_1, fee_estimate_mode);
52     (void)FormatParagraph(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 1000), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 1000));
53     (void)FormatSubVersion(random_string_1, fuzzed_data_provider.ConsumeIntegral<int>(), random_string_vector);
54     (void)GetDescriptorChecksum(random_string_1);
55     (void)HelpExampleCli(random_string_1, random_string_2);
56     (void)HelpExampleRpc(random_string_1, random_string_2);
57     (void)HelpMessageGroup(random_string_1);
58     (void)HelpMessageOpt(random_string_1, random_string_2);
59     (void)IsDeprecatedRPCEnabled(random_string_1);
60     (void)Join(random_string_vector, random_string_1);
61     (void)JSONRPCError(fuzzed_data_provider.ConsumeIntegral<int>(), random_string_1);
62     const util::Settings settings;
63     (void)OnlyHasDefaultSectionSetting(settings, random_string_1, random_string_2);
64     (void)ParseNetwork(random_string_1);
65     try {
66         (void)ParseNonRFCJSONValue(random_string_1);
67     } catch (const std::runtime_error&) {
68     }
69     OutputType output_type;
70     (void)ParseOutputType(random_string_1, output_type);
71     (void)RemovePrefix(random_string_1, random_string_2);
72     (void)ResolveErrMsg(random_string_1, random_string_2);
73     try {
74         (void)RPCConvertNamedValues(random_string_1, random_string_vector);
75     } catch (const std::runtime_error&) {
76     }
77     try {
78         (void)RPCConvertValues(random_string_1, random_string_vector);
79     } catch (const std::runtime_error&) {
80     }
81     (void)SanitizeString(random_string_1);
82     (void)SanitizeString(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 3));
83 #ifndef WIN32
84     (void)ShellEscape(random_string_1);
85 #endif // WIN32
86     uint16_t port_out;
87     std::string host_out;
88     SplitHostPort(random_string_1, port_out, host_out);
89     (void)TimingResistantEqual(random_string_1, random_string_2);
90     (void)ToLower(random_string_1);
91     (void)ToUpper(random_string_1);
92     (void)TrimString(random_string_1);
93     (void)TrimString(random_string_1, random_string_2);
94     (void)urlDecode(random_string_1);
95     (void)ValidAsCString(random_string_1);
96     (void)_(random_string_1.c_str());
97     try {
98         throw scriptnum_error{random_string_1};
99     } catch (const std::runtime_error&) {
100     }
101 
102     {
103         CDataStream data_stream{SER_NETWORK, INIT_PROTO_VERSION};
104         std::string s;
105         auto limited_string = LIMITED_STRING(s, 10);
106         data_stream << random_string_1;
107         try {
108             data_stream >> limited_string;
109             assert(data_stream.empty());
110             assert(s.size() <= random_string_1.size());
111             assert(s.size() <= 10);
112             if (!random_string_1.empty()) {
113                 assert(!s.empty());
114             }
115         } catch (const std::ios_base::failure&) {
116         }
117     }
118     {
119         CDataStream data_stream{SER_NETWORK, INIT_PROTO_VERSION};
120         const auto limited_string = LIMITED_STRING(random_string_1, 10);
121         data_stream << limited_string;
122         std::string deserialized_string;
123         data_stream >> deserialized_string;
124         assert(data_stream.empty());
125         assert(deserialized_string == random_string_1);
126     }
127     {
128         int64_t amount_out;
129         (void)ParseFixedPoint(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024), &amount_out);
130     }
131     {
132         (void)Untranslated(random_string_1);
133         const bilingual_str bs1{random_string_1, random_string_2};
134         const bilingual_str bs2{random_string_2, random_string_1};
135         (void)(bs1 + bs2);
136     }
137 }
138