FuzzImage.cpp 941 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright 2018 Google, LLC
  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/SkData.h"
  9. #include "include/core/SkImage.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkSurface.h"
  12. bool FuzzImageDecode(sk_sp<SkData> bytes) {
  13. auto img = SkImage::MakeFromEncoded(bytes);
  14. if (nullptr == img.get()) {
  15. return false;
  16. }
  17. auto s = SkSurface::MakeRasterN32Premul(128, 128);
  18. if (!s) {
  19. // May return nullptr in memory-constrained fuzzing environments
  20. return false;
  21. }
  22. SkPaint p;
  23. s->getCanvas()->drawImage(img, 0, 0, &p);
  24. return true;
  25. }
  26. #if defined(IS_FUZZING_WITH_LIBFUZZER)
  27. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  28. auto bytes = SkData::MakeWithoutCopy(data, size);
  29. FuzzImageDecode(bytes);
  30. return 0;
  31. }
  32. #endif