redaction_tool_fuzzer.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2020 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 <memory>
  7. #include <string>
  8. #include <vector>
  9. #include <fuzzer/FuzzedDataProvider.h>
  10. #include "components/feedback/redaction_tool.h"
  11. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  12. FuzzedDataProvider provider(data, size);
  13. int first_party_extension_id_count = provider.ConsumeIntegralInRange(-1, 50);
  14. // This is the storage for the strings inside first_party_extension_ids. This
  15. // is to make sure the char *'s we pass to the RedactionTool constructor are
  16. // deleted correctly -- they must be deleted after redactor is destructed, but
  17. // not leaked.
  18. std::vector<std::string> first_party_extension_id_store;
  19. // The first_party_extension_ids we pass to the RedactionTool constructor.
  20. // This owns the array but not the pointed-to strings. Note that if
  21. // first_party_extension_id_count is -1, this is not set so we pass nullptr to
  22. // the constructor; that's deliberate.
  23. std::unique_ptr<const char*[]> first_party_extension_ids;
  24. if (first_party_extension_id_count >= 0) {
  25. first_party_extension_id_store.reserve(first_party_extension_id_count);
  26. first_party_extension_ids =
  27. std::make_unique<const char*[]>(first_party_extension_id_count + 1);
  28. for (int i = 0; i < first_party_extension_id_count; ++i) {
  29. constexpr int kArbitraryMaxNameLength = 4096;
  30. first_party_extension_id_store.emplace_back(
  31. provider.ConsumeRandomLengthString(kArbitraryMaxNameLength));
  32. first_party_extension_ids[i] = first_party_extension_id_store[i].c_str();
  33. }
  34. first_party_extension_ids[first_party_extension_id_count] = nullptr;
  35. }
  36. feedback::RedactionTool redactor(first_party_extension_ids.get());
  37. redactor.Redact(provider.ConsumeRemainingBytesAsString());
  38. return 0;
  39. }