Bitcoin Core  31.0.0
P2P Digital Currency
string.cpp
Go to the documentation of this file.
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 <blockfilter.h>
6 #include <clientversion.h>
7 #include <common/args.h>
8 #include <common/messages.h>
9 #include <common/settings.h>
10 #include <common/system.h>
11 #include <common/url.h>
12 #include <netbase.h>
13 #include <outputtype.h>
14 #include <rpc/client.h>
15 #include <rpc/request.h>
16 #include <rpc/server.h>
17 #include <rpc/util.h>
18 #include <script/descriptor.h>
19 #include <script/script.h>
20 #include <serialize.h>
21 #include <streams.h>
23 #include <test/fuzz/fuzz.h>
24 #include <test/fuzz/util.h>
25 #include <util/fees.h>
26 #include <util/strencodings.h>
27 #include <util/string.h>
28 #include <util/translation.h>
29 
30 #include <cassert>
31 #include <cstdint>
32 #include <cstdlib>
33 #include <ios>
34 #include <stdexcept>
35 #include <string>
36 #include <vector>
37 
43 using util::Join;
44 using util::RemovePrefix;
45 using util::SplitString;
46 using util::TrimString;
47 
48 FUZZ_TARGET(string)
49 {
50  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
51  const std::string random_string_1 = fuzzed_data_provider.ConsumeRandomLengthString(32);
52  const std::string random_string_2 = fuzzed_data_provider.ConsumeRandomLengthString(32);
53  const std::vector<std::string> random_string_vector = ConsumeRandomLengthStringVector(fuzzed_data_provider);
54 
55  (void)AmountErrMsg(random_string_1, random_string_2);
56  (void)AmountHighWarn(random_string_1);
57  BlockFilterType block_filter_type;
58  (void)BlockFilterTypeByName(random_string_1, block_filter_type);
59  (void)Capitalize(random_string_1);
60  (void)CopyrightHolders(random_string_1);
61  FeeEstimateMode fee_estimate_mode;
62  (void)FeeModeFromString(random_string_1, fee_estimate_mode);
63  const auto width{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 1000)};
64  (void)FormatParagraph(random_string_1, width, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, width));
65  (void)FormatSubVersion(random_string_1, fuzzed_data_provider.ConsumeIntegral<int>(), random_string_vector);
66  (void)GetDescriptorChecksum(random_string_1);
67  (void)HelpExampleCli(random_string_1, random_string_2);
68  (void)HelpExampleRpc(random_string_1, random_string_2);
69  (void)HelpMessageGroup(random_string_1);
70  (void)HelpMessageOpt(random_string_1, random_string_2);
71  (void)IsDeprecatedRPCEnabled(random_string_1);
72  (void)Join(random_string_vector, random_string_1);
73  (void)JSONRPCError(fuzzed_data_provider.ConsumeIntegral<int>(), random_string_1);
74  const common::Settings settings;
75  (void)OnlyHasDefaultSectionSetting(settings, random_string_1, random_string_2);
76  (void)ParseNetwork(random_string_1);
77  (void)ParseOutputType(random_string_1);
78  (void)RemovePrefix(random_string_1, random_string_2);
79  (void)ResolveErrMsg(random_string_1, random_string_2);
80  try {
81  (void)RPCConvertNamedValues(random_string_1, random_string_vector);
82  } catch (const std::runtime_error&) {
83  }
84  try {
85  (void)RPCConvertValues(random_string_1, random_string_vector);
86  } catch (const std::runtime_error&) {
87  }
88  (void)SanitizeString(random_string_1);
89  (void)SanitizeString(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 3));
90 #ifndef WIN32
91  (void)ShellEscape(random_string_1);
92 #endif // WIN32
93  uint16_t port_out;
94  std::string host_out;
95  SplitHostPort(random_string_1, port_out, host_out);
96  (void)TimingResistantEqual(random_string_1, random_string_2);
97  (void)ToLower(random_string_1);
98  (void)ToUpper(random_string_1);
99  (void)TrimString(random_string_1);
100  (void)TrimString(random_string_1, random_string_2);
101  (void)UrlDecode(random_string_1);
102  (void)ContainsNoNUL(random_string_1);
103  try {
104  throw scriptnum_error{random_string_1};
105  } catch (const std::runtime_error&) {
106  }
107 
108  {
109  DataStream data_stream{};
110  std::string s;
111  auto limited_string = LIMITED_STRING(s, 10);
112  data_stream << random_string_1;
113  try {
114  data_stream >> limited_string;
115  assert(data_stream.empty());
116  assert(s.size() <= random_string_1.size());
117  assert(s.size() <= 10);
118  if (!random_string_1.empty()) {
119  assert(!s.empty());
120  }
121  } catch (const std::ios_base::failure&) {
122  }
123  }
124  {
125  DataStream data_stream{};
126  const auto limited_string = LIMITED_STRING(random_string_1, 10);
127  data_stream << limited_string;
128  std::string deserialized_string;
129  data_stream >> deserialized_string;
130  assert(data_stream.empty());
131  assert(deserialized_string == random_string_1);
132  }
133  {
134  int64_t amount_out;
135  (void)ParseFixedPoint(random_string_1, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 1024), &amount_out);
136  }
137  {
138  const auto single_split{SplitString(random_string_1, fuzzed_data_provider.ConsumeIntegral<char>())};
139  assert(single_split.size() >= 1);
140  const auto any_split{SplitString(random_string_1, random_string_2)};
141  assert(any_split.size() >= 1);
142  }
143  {
144  (void)Untranslated(random_string_1);
145  const bilingual_str bs1{random_string_1, random_string_2};
146  const bilingual_str bs2{random_string_2, random_string_1};
147  (void)(bs1 + bs2);
148  }
149  {
150  const ByteUnit all_units[] = {
152  ByteUnit::k,
153  ByteUnit::K,
154  ByteUnit::m,
155  ByteUnit::M,
156  ByteUnit::g,
157  ByteUnit::G,
158  ByteUnit::t,
160  };
161  ByteUnit default_multiplier = fuzzed_data_provider.PickValueInArray(all_units);
162  (void)ParseByteUnits(random_string_1, default_multiplier);
163  }
164 }
std::string CopyrightHolders(const std::string &strPrefix)
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
Definition: strencodings.h:203
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:149
bool BlockFilterTypeByName(std::string_view name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
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:73
std::string TrimString(std::string_view str, std::string_view pattern=" \\\)
Definition: string.h:169
assert(!tx.IsCoinBase())
Stored settings.
Definition: settings.h:32
Bilingual messages:
Definition: translation.h:24
bilingual_str AmountHighWarn(const std::string &optname)
Definition: messages.cpp:161
bool SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
Splits socket address string into host string and port value.
is a home for simple string functions returning descriptive messages that are used in RPC and GUI int...
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: messages.cpp:151
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
std::string RemovePrefix(std::string_view str, std::string_view prefix)
Definition: string.h:190
std::string HelpMessageGroup(const std::string &message)
Format a string to be used as group of options in help messages.
Definition: args.cpp:732
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:234
std::string UrlDecode(std::string_view url_encoded)
Definition: url.cpp:12
bilingual_str AmountErrMsg(const std::string &optname, const std::string &strValue)
Definition: messages.cpp:166
std::string HelpMessageOpt(const std::string &option, const std::string &message)
Format a string to be used as option description in help messages.
Definition: args.cpp:736
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert command lines arguments to params object when -named is disabled.
Definition: client.cpp:435
bool FeeModeFromString(std::string_view mode_string, FeeEstimateMode &fee_estimate_mode)
Definition: messages.cpp:93
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
BlockFilterType
Definition: blockfilter.h:93
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:201
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:70
FUZZ_TARGET(string)
Definition: string.cpp:48
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:41
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:132
std::string ConsumeRandomLengthString(size_t max_length)
std::optional< uint64_t > ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
Parse a string with suffix unit [k|K|m|M|g|G|t|T].
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...
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:183
bool IsDeprecatedRPCEnabled(const std::string &method)
Definition: server.cpp:339
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
Definition: strencodings.h:44
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
enum Network ParseNetwork(const std::string &net_in)
Definition: netbase.cpp:100
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:38
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:248
FeeEstimateMode
Definition: fees.h:9
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...
#define LIMITED_STRING(obj, n)
Definition: serialize.h:493
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert command line arguments to params object when -named is enabled.
Definition: client.cpp:475
T ConsumeIntegralInRange(T min, T max)
std::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
std::optional< OutputType > ParseOutputType(std::string_view type)
Definition: outputtype.cpp:23
T PickValueInArray(const T(&array)[size])
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:205
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.