favicon_url_parser_fuzzer.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2019 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 "base/at_exit.h"
  8. #include "base/i18n/icu_util.h"
  9. #include "components/favicon_base/favicon_url_parser.h"
  10. struct IcuEnvironment {
  11. IcuEnvironment() { CHECK(base::i18n::InitializeICU()); }
  12. // used by ICU integration.
  13. base::AtExitManager at_exit_manager;
  14. };
  15. IcuEnvironment* env = new IcuEnvironment();
  16. chrome::FaviconUrlFormat GetFaviconUrlFormatFromUint8(uint8_t value) {
  17. // Dummy switch to detect changes to the enum definition.
  18. switch (chrome::FaviconUrlFormat()) {
  19. case chrome::FaviconUrlFormat::kFaviconLegacy:
  20. case chrome::FaviconUrlFormat::kFavicon2:
  21. break;
  22. }
  23. return (value % 2) == 0 ? chrome::FaviconUrlFormat::kFaviconLegacy
  24. : chrome::FaviconUrlFormat::kFavicon2;
  25. }
  26. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  27. if (size < 2)
  28. return 0;
  29. // The first byte is used to determine the FaviconUrlFormat, and the rest of
  30. // the data is used in the input string to parse.
  31. const chrome::FaviconUrlFormat url_format =
  32. GetFaviconUrlFormatFromUint8(data[0]);
  33. const std::string string_input(reinterpret_cast<const char*>(data + 1),
  34. size - 1);
  35. chrome::ParsedFaviconPath parsed;
  36. chrome::ParseFaviconPath(string_input, url_format, &parsed);
  37. return 0;
  38. }