Debian Codegraph @

fuzz/oss_fuzz/FuzzPathDeserialize.cpp

raw
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
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkPaint.h"
10 #include "include/core/SkPath.h"
11 #include "include/core/SkSurface.h"
12 #include "src/core/SkReadBuffer.h"
13
14 void FuzzPathDeserialize(const uint8_t *data, size_t size) {
15 SkReadBuffer buf(data, size);
16
17 auto path = buf.readPath();
18 if (!buf.isValid()) {
19 SkASSERT(!path.has_value());
20 return;
21 }
22 SkASSERT(path.has_value());
23
24 auto s = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(128, 128));
25 if (!s) {
26 // May return nullptr in memory-constrained fuzzing environments
27 return;
28 }
29 s->getCanvas()->drawPath(*path, SkPaint());
30 }
31
32 #if defined(SK_BUILD_FOR_LIBFUZZER)
33 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
34 if (size < 4 || size > 2000) {
35 return 0;
36 }
37 uint32_t packed;
38 memcpy(&packed, data, 4);
39 unsigned version = packed & 0xFF;
40 if (version != 4) {
41 // Chrome only will produce version 4, so guide the fuzzer to
42 // only focus on those branches.
43 return 0;
44 }
45 FuzzPathDeserialize(data, size);
46 return 0;
47 }
48 #endif