FuzzPathDeserialize.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright 2018 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkPaint.h"
  9. #include "include/core/SkPath.h"
  10. #include "include/core/SkSurface.h"
  11. #include "src/core/SkReadBuffer.h"
  12. void FuzzPathDeserialize(SkReadBuffer& buf) {
  13. SkPath path;
  14. buf.readPath(&path);
  15. if (!buf.isValid()) {
  16. return;
  17. }
  18. auto s = SkSurface::MakeRasterN32Premul(128, 128);
  19. if (!s) {
  20. // May return nullptr in memory-constrained fuzzing environments
  21. return;
  22. }
  23. s->getCanvas()->drawPath(path, SkPaint());
  24. }
  25. #if defined(IS_FUZZING_WITH_LIBFUZZER)
  26. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  27. if (size < 4) {
  28. return 0;
  29. }
  30. uint32_t packed;
  31. memcpy(&packed, data, 4);
  32. unsigned version = packed & 0xFF;
  33. if (version != 4) {
  34. // Chrome only will produce version 4, so guide the fuzzer to
  35. // only focus on those branches.
  36. return 0;
  37. }
  38. SkReadBuffer buf(data, size);
  39. FuzzPathDeserialize(buf);
  40. return 0;
  41. }
  42. #endif