FuzzRegionDeserialize.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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/SkSurface.h"
  10. #include "src/core/SkRegionPriv.h"
  11. bool FuzzRegionDeserialize(sk_sp<SkData> bytes) {
  12. SkRegion region;
  13. if (!region.readFromMemory(bytes->data(), bytes->size())) {
  14. return false;
  15. }
  16. region.computeRegionComplexity();
  17. region.isComplex();
  18. SkRegion r2;
  19. if (region == r2) {
  20. region.contains(0,0);
  21. } else {
  22. region.contains(1,1);
  23. }
  24. auto s = SkSurface::MakeRasterN32Premul(128, 128);
  25. if (!s) {
  26. // May return nullptr in memory-constrained fuzzing environments
  27. return false;
  28. }
  29. s->getCanvas()->drawRegion(region, SkPaint());
  30. SkDEBUGCODE(SkRegionPriv::Validate(region));
  31. return true;
  32. }
  33. #if defined(IS_FUZZING_WITH_LIBFUZZER)
  34. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  35. auto bytes = SkData::MakeWithoutCopy(data, size);
  36. FuzzRegionDeserialize(bytes);
  37. return 0;
  38. }
  39. #endif