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 <string>
35 #include <vector>
36 
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  (void)ParseOutputType(random_string_1);
70  (void)RemovePrefix(random_string_1, random_string_2);
71  (void)ResolveErrMsg(random_string_1, random_string_2);
72  try {
73  (void)RPCConvertNamedValues(random_string_1, random_string_vector);
74  } catch (const std::runtime_error&) {
75  }
76  try {
77  (void)RPCConvertValues(random_string_1, random_string_vector);
78  } catch (const std::runtime_error&) {
79  }
80  (void)SanitizeString(random_string_1);
81  (void)SanitizeString(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 3));
82 #ifndef WIN32
83  (void)ShellEscape(random_string_1);
84 #endif // WIN32
85  uint16_t port_out;
86  std::string host_out;
87  SplitHostPort(random_string_1, port_out, host_out);
88  (void)TimingResistantEqual(random_string_1, random_string_2);
89  (void)ToLower(random_string_1);
90  (void)ToUpper(random_string_1);
91  (void)TrimString(random_string_1);
92  (void)TrimString(random_string_1, random_string_2);
93  (void)urlDecode(random_string_1);
94  (void)ValidAsCString(random_string_1);
95  (void)_(random_string_1.c_str());
96  try {
97  throw scriptnum_error{random_string_1};
98  } catch (const std::runtime_error&) {
99  }
100 
101  {
103  std::string s;
104  auto limited_string = LIMITED_STRING(s, 10);
105  data_stream << random_string_1;
106  try {
107  data_stream >> limited_string;
108  assert(data_stream.empty());
109  assert(s.size() <= random_string_1.size());
110  assert(s.size() <= 10);
111  if (!random_string_1.empty()) {
112  assert(!s.empty());
113  }
114  } catch (const std::ios_base::failure&) {
115  }
116  }
117  {
119  const auto limited_string = LIMITED_STRING(random_string_1, 10);
120  data_stream << limited_string;
121  std::string deserialized_string;
122  data_stream >> deserialized_string;
123  assert(data_stream.empty());
124  assert(deserialized_string == random_string_1);
125  }
126  {
127  int64_t amount_out;
128  (void)ParseFixedPoint(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024), &amount_out);
129  }
130  {
131  (void)Untranslated(random_string_1);
132  const bilingual_str bs1{random_string_1, random_string_2};
133  const bilingual_str bs2{random_string_2, random_string_1};
134  (void)(bs1 + bs2);
135  }
136 }
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:79
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:155
_
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:752
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:481
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:185
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:1349
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:756
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:240
ValidAsCString
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:78
version.h
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:1403
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:259
error.h
Capitalize
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
Definition: strencodings.cpp:587
ToUpper
std::string ToUpper(const std::string &str)
Returns the uppercase equivalent of the given string.
Definition: strencodings.cpp:580
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:173
script.h
TimingResistantEqual
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:164
system.h
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
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:37
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:204
BlockFilterType
BlockFilterType
Definition: blockfilter.h:88
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
util.h
netbase.h
IsDeprecatedRPCEnabled
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:352
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:231
LIMITED_STRING
#define LIMITED_STRING(obj, n)
Definition: serialize.h:445
ShellEscape
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:1234
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204
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:399
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:573