Branch data Line data Source code
1 : : // Copyright (c) 2020-present 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 <util/string.h>
6 : :
7 : : #include <blockfilter.h>
8 : : #include <clientversion.h>
9 : : #include <common/args.h>
10 : : #include <common/license_info.h>
11 : : #include <common/messages.h>
12 : : #include <common/settings.h>
13 : : #include <common/system.h>
14 : : #include <common/url.h>
15 : : #include <netbase.h>
16 : : #include <outputtype.h>
17 : : #include <rpc/client.h>
18 : : #include <rpc/request.h>
19 : : #include <rpc/server.h>
20 : : #include <rpc/util.h>
21 : : #include <script/descriptor.h>
22 : : #include <script/script.h>
23 : : #include <serialize.h>
24 : : #include <streams.h>
25 : : #include <test/fuzz/FuzzedDataProvider.h>
26 : : #include <test/fuzz/fuzz.h>
27 : : #include <test/fuzz/util.h>
28 : : #include <util/check.h>
29 : : #include <util/fees.h>
30 : : #include <util/strencodings.h>
31 : : #include <util/translation.h>
32 : :
33 : : #include <cstdint>
34 : : #include <cstdlib>
35 : : #include <ios>
36 : : #include <stdexcept>
37 : : #include <string>
38 : : #include <vector>
39 : :
40 : : using common::AmountErrMsg;
41 : : using common::AmountHighWarn;
42 : : using common::FeeModeFromString;
43 : : using common::ResolveErrMsg;
44 : : using util::ContainsNUL;
45 : : using util::Join;
46 : : using util::RemovePrefix;
47 : : using util::SplitString;
48 : : using util::TrimString;
49 : :
50 [ + - ]: 1644 : FUZZ_TARGET(string)
51 : : {
52 : 1168 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
53 : 1168 : const std::string random_string_1 = fuzzed_data_provider.ConsumeRandomLengthString(32);
54 [ + - ]: 1168 : const std::string random_string_2 = fuzzed_data_provider.ConsumeRandomLengthString(32);
55 : 1168 : const std::vector<std::string> random_string_vector = ConsumeRandomLengthStringVector(fuzzed_data_provider);
56 : :
57 [ + - ]: 1168 : (void)AmountErrMsg(random_string_1, random_string_2);
58 [ + - ]: 1168 : (void)AmountHighWarn(random_string_1);
59 : 1168 : BlockFilterType block_filter_type;
60 [ - + + - ]: 1168 : (void)BlockFilterTypeByName(random_string_1, block_filter_type);
61 [ - + + - ]: 3504 : (void)Capitalize(random_string_1);
62 [ + - ]: 1168 : (void)CopyrightHolders(random_string_1);
63 : 1168 : FeeEstimateMode fee_estimate_mode;
64 [ - + + - ]: 1168 : (void)FeeModeFromString(random_string_1, fee_estimate_mode);
65 : 1168 : const auto width{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 1000)};
66 [ - + + - ]: 1168 : (void)FormatParagraph(random_string_1, width, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, width));
67 [ + - ]: 1168 : (void)FormatSubVersion(random_string_1, fuzzed_data_provider.ConsumeIntegral<int>(), random_string_vector);
68 [ + - ]: 1168 : (void)HelpExampleCli(random_string_1, random_string_2);
69 [ + - ]: 1168 : (void)HelpExampleRpc(random_string_1, random_string_2);
70 [ + - ]: 1168 : (void)HelpMessageGroup(random_string_1);
71 [ - + - + : 1168 : (void)HelpMessageOpt(random_string_1, "", random_string_2);
+ - ]
72 [ - + - + : 1168 : (void)HelpMessageOpt(random_string_1, random_string_2, "");
+ - ]
73 [ + - ]: 1168 : (void)IsDeprecatedRPCEnabled(random_string_1);
74 [ + - ]: 1168 : (void)Join(random_string_vector, random_string_1);
75 [ + - ]: 1168 : (void)JSONRPCError(fuzzed_data_provider.ConsumeIntegral<int>(), random_string_1);
76 : 1168 : const common::Settings settings;
77 [ + - ]: 1168 : (void)OnlyHasDefaultSectionSetting(settings, random_string_1, random_string_2);
78 [ + - ]: 1168 : (void)ParseNetwork(random_string_1);
79 [ - + + - ]: 1168 : (void)ParseOutputType(random_string_1);
80 [ - + - + : 1168 : (void)RemovePrefix(random_string_1, random_string_2);
+ - ]
81 [ + - ]: 1168 : (void)ResolveErrMsg(random_string_1, random_string_2);
82 : 1168 : try {
83 [ + + ]: 1168 : (void)RPCConvertNamedValues(random_string_1, random_string_vector);
84 [ - + ]: 709 : } catch (const std::runtime_error&) {
85 : 709 : }
86 : 1168 : try {
87 [ + + ]: 1168 : (void)RPCConvertValues(random_string_1, random_string_vector);
88 [ - + ]: 746 : } catch (const std::runtime_error&) {
89 : 746 : }
90 [ - + + - ]: 1168 : (void)SanitizeString(random_string_1);
91 [ - + + - ]: 1168 : (void)SanitizeString(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 3));
92 : : #ifndef WIN32
93 [ + - ]: 1168 : (void)ShellEscape(random_string_1);
94 : : #endif // WIN32
95 : 1168 : uint16_t port_out;
96 [ - + ]: 1168 : std::string host_out;
97 [ - + + - ]: 1168 : SplitHostPort(random_string_1, port_out, host_out);
98 : 1168 : (void)TimingResistantEqual(random_string_1, random_string_2);
99 [ - + + - ]: 1168 : (void)ToLower(random_string_1);
100 [ - + + - ]: 1168 : (void)ToUpper(random_string_1);
101 [ - + + - ]: 1168 : (void)TrimString(random_string_1);
102 [ - + - + : 1168 : (void)TrimString(random_string_1, random_string_2);
+ - ]
103 [ - + + - ]: 1168 : (void)UrlDecode(random_string_1);
104 [ - + ]: 1168 : (void)ContainsNUL(random_string_1);
105 : 1168 : try {
106 [ + - ]: 1168 : throw scriptnum_error{random_string_1};
107 [ - + ]: 1168 : } catch (const std::runtime_error&) {
108 : 1168 : }
109 : :
110 : 1168 : {
111 : 1168 : DataStream data_stream{};
112 [ + - ]: 1168 : std::string s;
113 [ + - ]: 1168 : auto limited_string = LIMITED_STRING(s, 10);
114 [ + - ]: 1168 : data_stream << random_string_1;
115 : 1168 : try {
116 [ + + ]: 1168 : data_stream >> limited_string;
117 [ - + - + ]: 831 : assert(data_stream.empty());
118 [ - + - + : 831 : assert(s.size() <= random_string_1.size());
- + ]
119 [ - + ]: 831 : assert(s.size() <= 10);
120 [ + + ]: 831 : if (!random_string_1.empty()) {
121 [ - + ]: 795 : assert(!s.empty());
122 : : }
123 [ - + ]: 337 : } catch (const std::ios_base::failure&) {
124 : 337 : }
125 : 1168 : }
126 : 1168 : {
127 : 1168 : DataStream data_stream{};
128 [ + - ]: 1168 : const auto limited_string = LIMITED_STRING(random_string_1, 10);
129 [ + - ]: 1168 : data_stream << limited_string;
130 [ + - ]: 1168 : std::string deserialized_string;
131 [ + - ]: 1168 : data_stream >> deserialized_string;
132 [ - + - + ]: 1168 : assert(data_stream.empty());
133 [ - + ]: 1168 : assert(deserialized_string == random_string_1);
134 : 1168 : }
135 : 1168 : {
136 : 1168 : int64_t amount_out;
137 [ - + + - ]: 1168 : (void)ParseFixedPoint(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024), &amount_out);
138 : : }
139 : 1168 : {
140 [ - + + - ]: 1168 : const auto single_split{SplitString(random_string_1, fuzzed_data_provider.ConsumeIntegral<char>())};
141 [ - + - + ]: 1168 : assert(single_split.size() >= 1);
142 [ - + - + : 1168 : const auto any_split{SplitString(random_string_1, random_string_2)};
+ - ]
143 [ - + - + ]: 1168 : assert(any_split.size() >= 1);
144 : 1168 : }
145 : 1168 : {
146 [ - + + - ]: 3504 : (void)Untranslated(random_string_1);
147 [ - + - + ]: 2336 : const bilingual_str bs1{random_string_1, random_string_2};
148 [ - + - + ]: 2336 : const bilingual_str bs2{random_string_2, random_string_1};
149 [ + - ]: 4672 : (void)(bs1 + bs2);
150 : 1168 : }
151 : 1168 : {
152 : 1168 : const ByteUnit all_units[] = {
153 : : ByteUnit::NOOP,
154 : : ByteUnit::k,
155 : : ByteUnit::K,
156 : : ByteUnit::m,
157 : : ByteUnit::M,
158 : : ByteUnit::g,
159 : : ByteUnit::G,
160 : : ByteUnit::t,
161 : : ByteUnit::T
162 : : };
163 : 1168 : ByteUnit default_multiplier = fuzzed_data_provider.PickValueInArray(all_units);
164 [ - + + - ]: 1168 : (void)ParseByteUnits(random_string_1, default_multiplier);
165 : : }
166 : 1168 : }
|