usb_string_read_fuzzer.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 <string>
  7. #include "base/bind.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "services/device/usb/fake_usb_device_handle.h"
  10. #include "services/device/usb/usb_descriptors.h"
  11. namespace device {
  12. void Done(std::unique_ptr<std::map<uint8_t, std::u16string>> index_map) {}
  13. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  14. // Uses the first few bytes of the input to decide which strings to request.
  15. // Stops once it encounters 0 which is not a valid string index.
  16. std::unique_ptr<std::map<uint8_t, std::u16string>> index_map(
  17. new std::map<uint8_t, std::u16string>());
  18. for (size_t i = 0; i < size && data[i] != 0; i++)
  19. (*index_map)[data[i]] = std::u16string();
  20. size_t used = index_map->size() + 1;
  21. if (used > size)
  22. return 0;
  23. scoped_refptr<UsbDeviceHandle> device_handle =
  24. new FakeUsbDeviceHandle(data + used, size - used);
  25. ReadUsbStringDescriptors(device_handle, std::move(index_map),
  26. base::BindOnce(&Done));
  27. return 0;
  28. }
  29. } // namespace device