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 
38 namespace {
39 bool 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 
50 bool 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 
66 bool 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 
81 bool 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 
98 bool 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 
110 bool 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.
127 int64_t atoi64_legacy(const std::string& str)
128 {
129  return strtoll(str.c_str(), nullptr, 10);
130 }
131 }; // namespace
132 
133 FUZZ_TARGET(string)
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 locale_independent_atoi_result = LocaleIndependentAtoi<int>(random_string_1);
280  const int64_t atoi64_result = atoi64_legacy(random_string_1);
281  assert(locale_independent_atoi_result == std::clamp<int64_t>(atoi64_result, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
282  }
283 
284  {
285  const int64_t atoi64_result = atoi64_legacy(random_string_1);
286  const int64_t locale_independent_atoi_result = LocaleIndependentAtoi<int64_t>(random_string_1);
287  assert(atoi64_result == locale_independent_atoi_result);
288  }
289 }
urlDecode
UrlDecodeFn urlDecode
Definition: url.h:11
ConsumeRandomLengthStringVector
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:81
ParseInt64
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:304
FeeModeFromString
bool FeeModeFromString(const std::string &mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: fees.cpp:57
HelpExampleCli
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:156
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
HelpMessageGroup
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: system.cpp:754
assert
assert(!tx.IsCoinBase())
ParseNetwork
enum Network ParseNetwork(const std::string &net_in)
Definition: netbase.cpp:87
ParseFixedPoint
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
Definition: strencodings.cpp:397
util::Settings
Stored settings.
Definition: settings.h:31
streams.h
string.h
bilingual_str
Bilingual messages:
Definition: translation.h:16
outputtype.h
BlockFilterTypeByName
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
Definition: blockfilter.cpp:154
FormatSubVersion
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...
Definition: clientversion.cpp:59
util.h
FuzzedDataProvider::ConsumeRandomLengthString
std::string ConsumeRandomLengthString(size_t max_length)
Definition: FuzzedDataProvider.h:152
ResolveErrMsg
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: error.cpp:43
CopyrightHolders
std::string CopyrightHolders(const std::string &strPrefix)
Definition: system.cpp:1350
scriptnum_error
Definition: script.h:213
HelpMessageOpt
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
clientversion.h
TrimString
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:18
RPCConvertValues
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:246
ValidAsCString
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:78
atoi64_legacy
int64_t atoi64_legacy(const std::string &str)
Definition: util_tests.cpp:1626
version.h
IsSpace
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:152
AmountErrMsg
bilingual_str AmountErrMsg(const std::string &optname, const std::string &strValue)
Definition: error.cpp:53
GetDescriptorChecksum
std::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
Definition: descriptor.cpp:1404
AmountHighWarn
bilingual_str AmountHighWarn(const std::string &optname)
Definition: error.cpp:48
strencodings.h
settings.h
SplitHostPort
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
Definition: strencodings.cpp:110
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:46
message.h
FeeEstimateMode
FeeEstimateMode
Definition: feerate.h:18
netaddress.h
FuzzedDataProvider.h
RPCConvertNamedValues
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:265
u8
unsigned char u8
Definition: crypto_diff_fuzz_chacha20.cpp:21
error.h
Capitalize
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
Definition: strencodings.cpp:503
ToUpper
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
Definition: strencodings.cpp:496
RemovePrefix
std::string RemovePrefix(const std::string &str, const std::string &prefix)
Definition: string.h:28
request.h
HelpExampleRpc
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:174
script.h
TimingResistantEqual
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:237
system.h
ParseUInt64
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
Definition: strencodings.cpp:324
Join
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
ParseInt32
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:299
fees.h
SanitizeString
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
Definition: strencodings.cpp:27
FUZZ_TARGET
FUZZ_TARGET(string)
Definition: string.cpp:133
translation.h
url.h
fuzz.h
JSONRPCError
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:51
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
util::OnlyHasDefaultSectionSetting
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
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
BlockFilterType
BlockFilterType
Definition: blockfilter.h:88
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
netbase.h
IsDeprecatedRPCEnabled
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:352
u32
unsigned int u32
Definition: crypto_diff_fuzz_chacha20.cpp:20
server.h
ParseNonRFCJSONValue
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:237
ParseUInt8
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
Definition: strencodings.cpp:309
LIMITED_STRING
#define LIMITED_STRING(obj, n)
Definition: serialize.h:445
ShellEscape
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:1235
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204
ParseUInt32
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:319
ParseOutputType
std::optional< OutputType > ParseOutputType(const std::string &type)
Definition: outputtype.cpp:24
client.h
FormatParagraph
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.
Definition: strencodings.cpp:329
descriptor.h
INIT_PROTO_VERSION
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
ToLower
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
Definition: strencodings.cpp:489