hunspell_spell_fuzzer.cc 1.0 KB

12345678910111213141516171819202122232425262728293031
  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/strings/utf_string_conversions.h"
  8. #include "third_party/hunspell/fuzz/hunspell_fuzzer_hunspell_dictionary.h"
  9. #include "third_party/hunspell/src/hunspell/hunspell.hxx"
  10. // Entry point for LibFuzzer.
  11. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  12. if (!size)
  13. return 0;
  14. static Hunspell* hunspell =
  15. new Hunspell(kHunspellDictionary, sizeof(kHunspellDictionary));
  16. std::string data_string(reinterpret_cast<const char*>(data), size);
  17. // hunspell is not handling invalid UTF8. To avoid that, do the same thing
  18. // Chromium does - convert to UTF16, and back to UTF8. Valid UTF8 guaranteed.
  19. std::u16string utf16_string = base::UTF8ToUTF16(data_string);
  20. data_string = base::UTF16ToUTF8(utf16_string);
  21. hunspell->spell(data_string);
  22. return 0;
  23. }