Bitcoin Core  24.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 (!ContainsNoNUL(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  const auto width{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 1000)};
149  (void)FormatParagraph(random_string_1, width, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, width));
150  (void)FormatSubVersion(random_string_1, fuzzed_data_provider.ConsumeIntegral<int>(), random_string_vector);
151  (void)GetDescriptorChecksum(random_string_1);
152  (void)HelpExampleCli(random_string_1, random_string_2);
153  (void)HelpExampleRpc(random_string_1, random_string_2);
154  (void)HelpMessageGroup(random_string_1);
155  (void)HelpMessageOpt(random_string_1, random_string_2);
156  (void)IsDeprecatedRPCEnabled(random_string_1);
157  (void)Join(random_string_vector, random_string_1);
158  (void)JSONRPCError(fuzzed_data_provider.ConsumeIntegral<int>(), random_string_1);
159  const util::Settings settings;
160  (void)OnlyHasDefaultSectionSetting(settings, random_string_1, random_string_2);
161  (void)ParseNetwork(random_string_1);
162  try {
163  (void)ParseNonRFCJSONValue(random_string_1);
164  } catch (const std::runtime_error&) {
165  }
166  (void)ParseOutputType(random_string_1);
167  (void)RemovePrefix(random_string_1, random_string_2);
168  (void)ResolveErrMsg(random_string_1, random_string_2);
169  try {
170  (void)RPCConvertNamedValues(random_string_1, random_string_vector);
171  } catch (const std::runtime_error&) {
172  }
173  try {
174  (void)RPCConvertValues(random_string_1, random_string_vector);
175  } catch (const std::runtime_error&) {
176  }
177  (void)SanitizeString(random_string_1);
178  (void)SanitizeString(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 3));
179 #ifndef WIN32
180  (void)ShellEscape(random_string_1);
181 #endif // WIN32
182  uint16_t port_out;
183  std::string host_out;
184  SplitHostPort(random_string_1, port_out, host_out);
185  (void)TimingResistantEqual(random_string_1, random_string_2);
186  (void)ToLower(random_string_1);
187  (void)ToUpper(random_string_1);
188  (void)TrimString(random_string_1);
189  (void)TrimString(random_string_1, random_string_2);
190  (void)urlDecode(random_string_1);
191  (void)ContainsNoNUL(random_string_1);
192  (void)_(random_string_1.c_str());
193  try {
194  throw scriptnum_error{random_string_1};
195  } catch (const std::runtime_error&) {
196  }
197 
198  {
200  std::string s;
201  auto limited_string = LIMITED_STRING(s, 10);
202  data_stream << random_string_1;
203  try {
204  data_stream >> limited_string;
205  assert(data_stream.empty());
206  assert(s.size() <= random_string_1.size());
207  assert(s.size() <= 10);
208  if (!random_string_1.empty()) {
209  assert(!s.empty());
210  }
211  } catch (const std::ios_base::failure&) {
212  }
213  }
214  {
216  const auto limited_string = LIMITED_STRING(random_string_1, 10);
217  data_stream << limited_string;
218  std::string deserialized_string;
219  data_stream >> deserialized_string;
220  assert(data_stream.empty());
221  assert(deserialized_string == random_string_1);
222  }
223  {
224  int64_t amount_out;
225  (void)ParseFixedPoint(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024), &amount_out);
226  }
227  {
228  const auto single_split{SplitString(random_string_1, fuzzed_data_provider.ConsumeIntegral<char>())};
229  assert(single_split.size() >= 1);
230  const auto any_split{SplitString(random_string_1, random_string_2)};
231  assert(any_split.size() >= 1);
232  }
233  {
234  (void)Untranslated(random_string_1);
235  const bilingual_str bs1{random_string_1, random_string_2};
236  const bilingual_str bs2{random_string_2, random_string_1};
237  (void)(bs1 + bs2);
238  }
239  {
240  int32_t i32;
241  int64_t i64;
242  uint32_t u32;
243  uint64_t u64;
244  uint8_t u8;
245  const bool ok_i32 = ParseInt32(random_string_1, &i32);
246  const bool ok_i64 = ParseInt64(random_string_1, &i64);
247  const bool ok_u32 = ParseUInt32(random_string_1, &u32);
248  const bool ok_u64 = ParseUInt64(random_string_1, &u64);
249  const bool ok_u8 = ParseUInt8(random_string_1, &u8);
250 
251  int32_t i32_legacy;
252  int64_t i64_legacy;
253  uint32_t u32_legacy;
254  uint64_t u64_legacy;
255  uint8_t u8_legacy;
256  const bool ok_i32_legacy = LegacyParseInt32(random_string_1, &i32_legacy);
257  const bool ok_i64_legacy = LegacyParseInt64(random_string_1, &i64_legacy);
258  const bool ok_u32_legacy = LegacyParseUInt32(random_string_1, &u32_legacy);
259  const bool ok_u64_legacy = LegacyParseUInt64(random_string_1, &u64_legacy);
260  const bool ok_u8_legacy = LegacyParseUInt8(random_string_1, &u8_legacy);
261 
262  assert(ok_i32 == ok_i32_legacy);
263  assert(ok_i64 == ok_i64_legacy);
264  assert(ok_u32 == ok_u32_legacy);
265  assert(ok_u64 == ok_u64_legacy);
266  assert(ok_u8 == ok_u8_legacy);
267 
268  if (ok_i32) {
269  assert(i32 == i32_legacy);
270  }
271  if (ok_i64) {
272  assert(i64 == i64_legacy);
273  }
274  if (ok_u32) {
275  assert(u32 == u32_legacy);
276  }
277  if (ok_u64) {
278  assert(u64 == u64_legacy);
279  }
280  if (ok_u8) {
281  assert(u8 == u8_legacy);
282  }
283  }
284 
285  {
286  const int locale_independent_atoi_result = LocaleIndependentAtoi<int>(random_string_1);
287  const int64_t atoi64_result = atoi64_legacy(random_string_1);
288  assert(locale_independent_atoi_result == std::clamp<int64_t>(atoi64_result, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
289  }
290 
291  {
292  const int64_t atoi64_result = atoi64_legacy(random_string_1);
293  const int64_t locale_independent_atoi_result = LocaleIndependentAtoi<int64_t>(random_string_1);
294  assert(atoi64_result == locale_independent_atoi_result);
295  }
296 }
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
BlockFilterType
Definition: blockfilter.h:90
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
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:246
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:255
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:274
std::string CopyrightHolders(const std::string &strPrefix)
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...
unsigned int u32
unsigned char u8
std::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
bilingual_str AmountHighWarn(const std::string &optname)
Definition: error.cpp:52
bilingual_str AmountErrMsg(const std::string &optname, const std::string &strValue)
Definition: error.cpp:57
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: error.cpp:47
FeeEstimateMode
Definition: feerate.h:21
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:228
enum Network ParseNetwork(const std::string &net_in)
Definition: netbase.cpp:87
std::optional< OutputType > ParseOutputType(const std::string &type)
Definition: outputtype.cpp:25
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:56
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:166
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:184
@ SER_NETWORK
Definition: serialize.h:131
#define LIMITED_STRING(obj, n)
Definition: serialize.h:438
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:349
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:238
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:153
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:41
std::string RemovePrefix(std::string_view str, std::string_view prefix)
Definition: string.h:54
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:21
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:97
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:68
Bilingual messages:
Definition: translation.h:18
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:128
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:65
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:48
UrlDecodeFn urlDecode
Definition: url.h:11
bool FeeModeFromString(const std::string &mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: fees.cpp:57
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool ParseUInt8(std::string_view str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
bool ParseUInt64(std::string_view str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
bool ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
void SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: system.cpp:823
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:827
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:1313
int64_t atoi64_legacy(const std::string &str)
assert(!tx.IsCoinBase())
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15