| 6 | #include <stdint.h> |
| 7 |
| 8 | #include <string> |
| 9 |
| 10 | #include "base/strings/string_tokenizer.h" |
| 11 |
| 12 | void GetAllTokens(base::StringTokenizer& t) { |
| 13 | while (t.GetNext()) { |
| 14 | (void)t.token(); |
| 15 | } |
| 16 | } |
| 17 |
| 18 | // Entry point for LibFuzzer. |
| 19 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 20 | uint8_t size_t_bytes = sizeof(size_t); |
| 21 | if (size < size_t_bytes + 1) { |
| 22 | return 0; |
| 23 | } |
| 24 |
| 25 | // Calculate pattern size based on remaining bytes, otherwise fuzzing is |
| 26 | // inefficient with bailouts in most cases. |
| 27 | size_t pattern_size = |
| 28 | *reinterpret_cast<const size_t*>(data) % (size - size_t_bytes); |
| 29 |
| 30 | std::string pattern(reinterpret_cast<const char*>(data + size_t_bytes), |
| 31 | pattern_size); |
| 32 | std::string input( |
| 33 | reinterpret_cast<const char*>(data + size_t_bytes + pattern_size), |
| 34 | size - pattern_size - size_t_bytes); |
| 35 |
| 36 | // Allow quote_chars and options to be set. Otherwise full coverage |
| 37 | // won't be possible since IsQuote, FullGetNext and other functions |
| 38 | // won't be called. |
| 39 | for (bool return_delims : {false, true}) { |
| 40 | for (bool return_empty_strings : {false, true}) { |
| 41 | int options = 0; |
| 42 | if (return_delims) |
| 43 | options |= base::StringTokenizer::RETURN_DELIMS; |
| 44 | if (return_empty_strings) |
| 45 | options |= base::StringTokenizer::RETURN_EMPTY_TOKENS; |
| 46 |
| 47 | base::StringTokenizer t(input, pattern); |
| 48 | t.set_options(options); |
| 49 | GetAllTokens(t); |
| 50 |
| 51 | base::StringTokenizer t_quote(input, pattern); |
| 52 | t_quote.set_quote_chars("\""); |
| 53 | t_quote.set_options(options); |
| 54 | GetAllTokens(t_quote); |
| 55 | } |
| 56 | } |
| 57 |
| 58 | return 0; |
| 59 | } |
| 60 |