Bitcoin Core 22.99.0
P2P Digital Currency
string.cpp
Go to the documentation of this file.
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>
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 <cstdlib>
35#include <string>
36#include <vector>
37
38namespace {
39bool LegacyParsePrechecks(const std::string& str)
40{
41 if (str.empty()) // No empty string allowed
42 return false;
43 if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size() - 1]))) // No padding allowed
44 return false;
45 if (!ValidAsCString(str)) // No embedded NUL characters allowed
46 return false;
47 return true;
48}
49
50bool LegacyParseInt32(const std::string& str, int32_t* out)
51{
52 if (!LegacyParsePrechecks(str))
53 return false;
54 char* endp = nullptr;
55 errno = 0; // strtol will not set errno if valid
56 long int n = strtol(str.c_str(), &endp, 10);
57 if (out) *out = (int32_t)n;
58 // Note that strtol returns a *long int*, so even if strtol doesn't report an over/underflow
59 // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
60 // platforms the size of these types may be different.
61 return endp && *endp == 0 && !errno &&
62 n >= std::numeric_limits<int32_t>::min() &&
63 n <= std::numeric_limits<int32_t>::max();
64}
65
66bool LegacyParseInt64(const std::string& str, int64_t* out)
67{
68 if (!LegacyParsePrechecks(str))
69 return false;
70 char* endp = nullptr;
71 errno = 0; // strtoll will not set errno if valid
72 long long int n = strtoll(str.c_str(), &endp, 10);
73 if (out) *out = (int64_t)n;
74 // Note that strtoll returns a *long long int*, so even if strtol doesn't report an over/underflow
75 // we still have to check that the returned value is within the range of an *int64_t*.
76 return endp && *endp == 0 && !errno &&
77 n >= std::numeric_limits<int64_t>::min() &&
78 n <= std::numeric_limits<int64_t>::max();
79}
80
81bool LegacyParseUInt32(const std::string& str, uint32_t* out)
82{
83 if (!LegacyParsePrechecks(str))
84 return false;
85 if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoul accepts these by default if they fit in the range
86 return false;
87 char* endp = nullptr;
88 errno = 0; // strtoul will not set errno if valid
89 unsigned long int n = strtoul(str.c_str(), &endp, 10);
90 if (out) *out = (uint32_t)n;
91 // Note that strtoul returns a *unsigned long int*, so even if it doesn't report an over/underflow
92 // we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit
93 // platforms the size of these types may be different.
94 return endp && *endp == 0 && !errno &&
95 n <= std::numeric_limits<uint32_t>::max();
96}
97
98bool LegacyParseUInt8(const std::string& str, uint8_t* out)
99{
100 uint32_t u32;
101 if (!LegacyParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
102 return false;
103 }
104 if (out != nullptr) {
105 *out = static_cast<uint8_t>(u32);
106 }
107 return true;
108}
109
110bool LegacyParseUInt64(const std::string& str, uint64_t* out)
111{
112 if (!LegacyParsePrechecks(str))
113 return false;
114 if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoull accepts these by default if they fit in the range
115 return false;
116 char* endp = nullptr;
117 errno = 0; // strtoull will not set errno if valid
118 unsigned long long int n = strtoull(str.c_str(), &endp, 10);
119 if (out) *out = (uint64_t)n;
120 // Note that strtoull returns a *unsigned long long int*, so even if it doesn't report an over/underflow
121 // we still have to check that the returned value is within the range of an *uint64_t*.
122 return endp && *endp == 0 && !errno &&
123 n <= std::numeric_limits<uint64_t>::max();
124}
125
126// For backwards compatibility checking.
127int64_t atoi64_legacy(const std::string& str)
128{
129 return strtoll(str.c_str(), nullptr, 10);
130}
131}; // namespace
132
134{
135 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
136 const std::string random_string_1 = fuzzed_data_provider.ConsumeRandomLengthString(32);
137 const std::string random_string_2 = fuzzed_data_provider.ConsumeRandomLengthString(32);
138 const std::vector<std::string> random_string_vector = ConsumeRandomLengthStringVector(fuzzed_data_provider);
139
140 (void)AmountErrMsg(random_string_1, random_string_2);
141 (void)AmountHighWarn(random_string_1);
142 BlockFilterType block_filter_type;
143 (void)BlockFilterTypeByName(random_string_1, block_filter_type);
144 (void)Capitalize(random_string_1);
145 (void)CopyrightHolders(random_string_1);
146 FeeEstimateMode fee_estimate_mode;
147 (void)FeeModeFromString(random_string_1, fee_estimate_mode);
148 (void)FormatParagraph(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 1000), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 1000));
149 (void)FormatSubVersion(random_string_1, fuzzed_data_provider.ConsumeIntegral<int>(), random_string_vector);
150 (void)GetDescriptorChecksum(random_string_1);
151 (void)HelpExampleCli(random_string_1, random_string_2);
152 (void)HelpExampleRpc(random_string_1, random_string_2);
153 (void)HelpMessageGroup(random_string_1);
154 (void)HelpMessageOpt(random_string_1, random_string_2);
155 (void)IsDeprecatedRPCEnabled(random_string_1);
156 (void)Join(random_string_vector, random_string_1);
157 (void)JSONRPCError(fuzzed_data_provider.ConsumeIntegral<int>(), random_string_1);
158 const util::Settings settings;
159 (void)OnlyHasDefaultSectionSetting(settings, random_string_1, random_string_2);
160 (void)ParseNetwork(random_string_1);
161 try {
162 (void)ParseNonRFCJSONValue(random_string_1);
163 } catch (const std::runtime_error&) {
164 }
165 (void)ParseOutputType(random_string_1);
166 (void)RemovePrefix(random_string_1, random_string_2);
167 (void)ResolveErrMsg(random_string_1, random_string_2);
168 try {
169 (void)RPCConvertNamedValues(random_string_1, random_string_vector);
170 } catch (const std::runtime_error&) {
171 }
172 try {
173 (void)RPCConvertValues(random_string_1, random_string_vector);
174 } catch (const std::runtime_error&) {
175 }
176 (void)SanitizeString(random_string_1);
177 (void)SanitizeString(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 3));
178#ifndef WIN32
179 (void)ShellEscape(random_string_1);
180#endif // WIN32
181 uint16_t port_out;
182 std::string host_out;
183 SplitHostPort(random_string_1, port_out, host_out);
184 (void)TimingResistantEqual(random_string_1, random_string_2);
185 (void)ToLower(random_string_1);
186 (void)ToUpper(random_string_1);
187 (void)TrimString(random_string_1);
188 (void)TrimString(random_string_1, random_string_2);
189 (void)urlDecode(random_string_1);
190 (void)ValidAsCString(random_string_1);
191 (void)_(random_string_1.c_str());
192 try {
193 throw scriptnum_error{random_string_1};
194 } catch (const std::runtime_error&) {
195 }
196
197 {
199 std::string s;
200 auto limited_string = LIMITED_STRING(s, 10);
201 data_stream << random_string_1;
202 try {
203 data_stream >> limited_string;
204 assert(data_stream.empty());
205 assert(s.size() <= random_string_1.size());
206 assert(s.size() <= 10);
207 if (!random_string_1.empty()) {
208 assert(!s.empty());
209 }
210 } catch (const std::ios_base::failure&) {
211 }
212 }
213 {
215 const auto limited_string = LIMITED_STRING(random_string_1, 10);
216 data_stream << limited_string;
217 std::string deserialized_string;
218 data_stream >> deserialized_string;
219 assert(data_stream.empty());
220 assert(deserialized_string == random_string_1);
221 }
222 {
223 int64_t amount_out;
224 (void)ParseFixedPoint(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024), &amount_out);
225 }
226 {
227 (void)Untranslated(random_string_1);
228 const bilingual_str bs1{random_string_1, random_string_2};
229 const bilingual_str bs2{random_string_2, random_string_1};
230 (void)(bs1 + bs2);
231 }
232 {
233 int32_t i32;
234 int64_t i64;
235 uint32_t u32;
236 uint64_t u64;
237 uint8_t u8;
238 const bool ok_i32 = ParseInt32(random_string_1, &i32);
239 const bool ok_i64 = ParseInt64(random_string_1, &i64);
240 const bool ok_u32 = ParseUInt32(random_string_1, &u32);
241 const bool ok_u64 = ParseUInt64(random_string_1, &u64);
242 const bool ok_u8 = ParseUInt8(random_string_1, &u8);
243
244 int32_t i32_legacy;
245 int64_t i64_legacy;
246 uint32_t u32_legacy;
247 uint64_t u64_legacy;
248 uint8_t u8_legacy;
249 const bool ok_i32_legacy = LegacyParseInt32(random_string_1, &i32_legacy);
250 const bool ok_i64_legacy = LegacyParseInt64(random_string_1, &i64_legacy);
251 const bool ok_u32_legacy = LegacyParseUInt32(random_string_1, &u32_legacy);
252 const bool ok_u64_legacy = LegacyParseUInt64(random_string_1, &u64_legacy);
253 const bool ok_u8_legacy = LegacyParseUInt8(random_string_1, &u8_legacy);
254
255 assert(ok_i32 == ok_i32_legacy);
256 assert(ok_i64 == ok_i64_legacy);
257 assert(ok_u32 == ok_u32_legacy);
258 assert(ok_u64 == ok_u64_legacy);
259 assert(ok_u8 == ok_u8_legacy);
260
261 if (ok_i32) {
262 assert(i32 == i32_legacy);
263 }
264 if (ok_i64) {
265 assert(i64 == i64_legacy);
266 }
267 if (ok_u32) {
268 assert(u32 == u32_legacy);
269 }
270 if (ok_u64) {
271 assert(u64 == u64_legacy);
272 }
273 if (ok_u8) {
274 assert(u8 == u8_legacy);
275 }
276 }
277
278 {
279 const int atoi_result = atoi(random_string_1.c_str());
280 const int locale_independent_atoi_result = LocaleIndependentAtoi<int>(random_string_1);
281 const int64_t atoi64_result = atoi64_legacy(random_string_1);
282 const bool out_of_range = atoi64_result < std::numeric_limits<int>::min() || atoi64_result > std::numeric_limits<int>::max();
283 if (out_of_range) {
284 assert(locale_independent_atoi_result == 0);
285 } else {
286 assert(atoi_result == locale_independent_atoi_result);
287 }
288 }
289
290 {
291 const int64_t atoi64_result = atoi64_legacy(random_string_1);
292 const int64_t locale_independent_atoi_result = LocaleIndependentAtoi<int64_t>(random_string_1);
293 assert(atoi64_result == locale_independent_atoi_result || locale_independent_atoi_result == 0);
294 }
295}
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
BlockFilterType
Definition: blockfilter.h:89
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
std::string ConsumeRandomLengthString(size_t max_length)
T ConsumeIntegralInRange(T min, T max)
UniValue ParseNonRFCJSONValue(const std::string &strVal)
Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) as well as obje...
Definition: client.cpp:231
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:240
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:259
std::string FormatSubVersion(const std::string &name, int nClientVersion, const std::vector< std::string > &comments)
Format the subversion field according to BIP 14 spec (https://github.com/bitcoin/bips/blob/master/bip...
std::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
bilingual_str AmountHighWarn(const std::string &optname)
Definition: error.cpp:48
bilingual_str AmountErrMsg(const std::string &optname, const std::string &strValue)
Definition: error.cpp:53
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: error.cpp:43
FeeEstimateMode
Definition: feerate.h:18
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string &section, const std::string &name)
Return true if a setting is set in the default config file section, and not overridden by a higher pr...
Definition: settings.cpp:218
enum Network ParseNetwork(const std::string &net_in)
Definition: netbase.cpp:87
std::optional< OutputType > ParseOutputType(const std::string &type)
Definition: outputtype.cpp:24
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:51
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:156
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:174
@ SER_NETWORK
Definition: serialize.h:138
#define LIMITED_STRING(obj, n)
Definition: serialize.h:445
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:352
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string FormatParagraph(const std::string &in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:205
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:121
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:44
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:78
std::string RemovePrefix(const std::string &str, const std::string &prefix)
Definition: string.h:28
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:18
Bilingual messages:
Definition: translation.h:16
Stored settings.
Definition: settings.h:31
FUZZ_TARGET(string)
Definition: string.cpp:133
std::vector< std::string > ConsumeRandomLengthStringVector(FuzzedDataProvider &fuzzed_data_provider, const size_t max_vector_size=16, const size_t max_string_length=16) noexcept
Definition: util.h:79
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:46
UrlDecodeFn urlDecode
Definition: url.h:11
bool FeeModeFromString(const std::string &mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: fees.cpp:57
std::string CopyrightHolders(const std::string &strPrefix)
Definition: system.cpp:1350
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: system.cpp:754
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
Definition: system.cpp:758
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:1235
assert(!tx.IsCoinBase())
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15