parse_url_hostname_to_address_fuzzer.cc 1020 B

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2016 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 <functional>
  7. #include "base/strings/string_piece.h"
  8. #include "net/base/address_list.h"
  9. #include "net/base/ip_address.h"
  10. // Entry point for LibFuzzer.
  11. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  12. const base::StringPiece hostname(reinterpret_cast<const char*>(data), size);
  13. net::IPAddress address;
  14. if (net::ParseURLHostnameToAddress(hostname, &address)) {
  15. // To fuzz port number without spending raw bytes of data, use hash(data).
  16. std::size_t data_hash = std::hash<std::string>()(std::string(hostname));
  17. uint16_t port = static_cast<uint16_t>(data_hash & 0xFFFF);
  18. net::AddressList addresses =
  19. net::AddressList::CreateFromIPAddress(address, port);
  20. for (const auto& endpoint : addresses) {
  21. endpoint.ToString();
  22. }
  23. }
  24. return 0;
  25. }