xml_parser_fuzzer.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2017 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/run_loop.h"
  9. #include "base/task/single_thread_task_executor.h"
  10. #include "services/data_decoder/xml_parser.h"
  11. #include "third_party/libxml/chromium/libxml_utils.h"
  12. namespace {
  13. void OnParseXml(base::OnceClosure quit_loop,
  14. absl::optional<base::Value> value,
  15. const absl::optional<std::string>& error) {
  16. std::move(quit_loop).Run();
  17. }
  18. // Error handler to ignore spamy messages from libxml.
  19. void ignore(void* ctx, const char* msg, ...) {}
  20. } // namespace
  21. static ScopedXmlErrorFunc scoped_xml_error_func(nullptr, &ignore);
  22. // Entry point for LibFuzzer.
  23. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  24. const char* data_ptr = reinterpret_cast<const char*>(data);
  25. data_decoder::XmlParser xml_parser_impl;
  26. data_decoder::mojom::XmlParser& xml_parser = xml_parser_impl;
  27. base::SingleThreadTaskExecutor main_thread_task_executor;
  28. base::RunLoop run_loop;
  29. xml_parser.Parse(std::string(data_ptr, size),
  30. data_decoder::mojom::XmlParser::WhitespaceBehavior::kIgnore,
  31. base::BindOnce(&OnParseXml, run_loop.QuitClosure()));
  32. run_loop.Run();
  33. return 0;
  34. }