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|.
| 150 | // with respect to a fuzzer inserting characters than just picking a random |
| 151 | // length and then consuming that many bytes with |ConsumeBytes|. |
| 152 | inline std::string |
| 153 | FuzzedDataProvider::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_|. |
| 181 | inline std::string FuzzedDataProvider::ConsumeRandomLengthString() { |
no test coverage detected