string_tokenizer_fuzzer.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <string>
  7. #include <tuple>
  8. #include "base/strings/string_tokenizer.h"
  9. void GetAllTokens(base::StringTokenizer& t) {
  10. while (t.GetNext()) {
  11. std::ignore = t.token();
  12. }
  13. }
  14. // Entry point for LibFuzzer.
  15. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  16. uint8_t size_t_bytes = sizeof(size_t);
  17. if (size < size_t_bytes + 1) {
  18. return 0;
  19. }
  20. // Calculate pattern size based on remaining bytes, otherwise fuzzing is
  21. // inefficient with bailouts in most cases.
  22. size_t pattern_size =
  23. *reinterpret_cast<const size_t*>(data) % (size - size_t_bytes);
  24. std::string pattern(reinterpret_cast<const char*>(data + size_t_bytes),
  25. pattern_size);
  26. std::string input(
  27. reinterpret_cast<const char*>(data + size_t_bytes + pattern_size),
  28. size - pattern_size - size_t_bytes);
  29. // Allow quote_chars and options to be set. Otherwise full coverage
  30. // won't be possible since IsQuote, FullGetNext and other functions
  31. // won't be called.
  32. for (bool return_delims : {false, true}) {
  33. for (bool return_empty_strings : {false, true}) {
  34. int options = 0;
  35. if (return_delims)
  36. options |= base::StringTokenizer::RETURN_DELIMS;
  37. if (return_empty_strings)
  38. options |= base::StringTokenizer::RETURN_EMPTY_TOKENS;
  39. base::StringTokenizer t(input, pattern);
  40. t.set_options(options);
  41. GetAllTokens(t);
  42. base::StringTokenizer t_quote(input, pattern);
  43. t_quote.set_quote_chars("\"");
  44. t_quote.set_options(options);
  45. GetAllTokens(t_quote);
  46. }
  47. }
  48. return 0;
  49. }