hunspell_suggest_fuzzer.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 <fuzzer/FuzzedDataProvider.h>
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "third_party/hunspell/fuzz/hunspell_fuzzer_hunspell_dictionary.h"
  10. #include "third_party/hunspell/src/hunspell/hunspell.hxx"
  11. // Entry point for LibFuzzer.
  12. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  13. if (!size)
  14. return 0;
  15. // Generate and use a range of dictionary sizes. Large dictionaries might have
  16. // more coverage and uncover more interesting code paths, however, it is
  17. // extremely slow. Smaller dictionaries are _much_ faster, but may result in
  18. // less coverage.
  19. FuzzedDataProvider data_provider(data, size);
  20. size_t shift = data_provider.ConsumeIntegralInRange(0, 16);
  21. static Hunspell* hunspell =
  22. new Hunspell(kHunspellDictionary, sizeof(kHunspellDictionary) >> shift);
  23. std::string data_string(reinterpret_cast<const char*>(data), size);
  24. // hunspell is not handling invalid UTF8. To avoid that, do the same thing
  25. // Chromium does - convert to UTF16, and back to UTF8. Valid UTF8 guaranteed.
  26. std::u16string utf16_string = base::UTF8ToUTF16(data_string);
  27. data_string = base::UTF16ToUTF8(utf16_string);
  28. std::vector<std::string> suggestions = hunspell->suggest(data_string);
  29. return 0;
  30. }