MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ConsumeRandomLengthString

Method ConsumeRandomLengthString

src/test/fuzz/FuzzedDataProvider.h:152–178  ·  view source on GitHub ↗

Returns a std::string of length from 0 to |max_length|. When it runs out of input data, returns what remains of the input. Designed to be more stable with respect to a fuzzer inserting characters than just picking a random length and then consuming that many bytes with |ConsumeBytes|.

Source from the content-addressed store, hash-verified

150// with respect to a fuzzer inserting characters than just picking a random
151// length and then consuming that many bytes with |ConsumeBytes|.
152inline std::string
153FuzzedDataProvider::ConsumeRandomLengthString(size_t max_length) {
154 // Reads bytes from the start of |data_ptr_|. Maps "\\" to "\", and maps "\"
155 // followed by anything else to the end of the string. As a result of this
156 // logic, a fuzzer can insert characters into the string, and the string
157 // will be lengthened to include those new characters, resulting in a more
158 // stable fuzzer than picking the length of a string independently from
159 // picking its contents.
160 std::string result;
161
162 // Reserve the anticipated capacity to prevent several reallocations.
163 result.reserve(std::min(max_length, remaining_bytes_));
164 for (size_t i = 0; i < max_length && remaining_bytes_ != 0; ++i) {
165 char next = ConvertUnsignedToSigned<char>(data_ptr_[0]);
166 Advance(1);
167 if (next == '\\' && remaining_bytes_ != 0) {
168 next = ConvertUnsignedToSigned<char>(data_ptr_[0]);
169 Advance(1);
170 if (next != '\\')
171 break;
172 }
173 result += next;
174 }
175
176 result.shrink_to_fit();
177 return result;
178}
179
180// Returns a std::string of length from 0 to |remaining_bytes_|.
181inline std::string FuzzedDataProvider::ConsumeRandomLengthString() {

Callers 15

CreateWalletDescriptorFunction · 0.80
crypter.cppFile · 0.80
block_index.cppFile · 0.80
FUZZ_TARGETFunction · 0.80
socks5.cppFile · 0.80
FUZZ_TARGETFunction · 0.80
FUZZ_TARGETFunction · 0.80
banman.cppFile · 0.80
FUZZ_TARGETFunction · 0.80
FUZZ_TARGETFunction · 0.80
FUZZ_TARGETFunction · 0.80

Calls 2

reserveMethod · 0.45
shrink_to_fitMethod · 0.45

Tested by

no test coverage detected