/src/skia/fuzz/oss_fuzz/FuzzIncrementalImage.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
8 | | #include "include/codec/SkCodec.h" |
9 | | #include "include/core/SkBitmap.h" |
10 | | #include "include/core/SkStream.h" |
11 | | #include "include/private/base/SkTemplates.h" |
12 | | |
13 | 9.92k | bool FuzzIncrementalImageDecode(const uint8_t *data, size_t size) { |
14 | 9.92k | auto codec = SkCodec::MakeFromStream(SkMemoryStream::MakeDirect(data, size)); |
15 | 9.92k | if (!codec) { |
16 | 6.67k | return false; |
17 | 6.67k | } |
18 | | |
19 | 3.25k | SkBitmap bm; |
20 | 3.25k | if (!bm.tryAllocPixels(codec->getInfo())) { |
21 | | // May fail in memory-constrained fuzzing environments |
22 | 584 | return false; |
23 | 584 | } |
24 | | |
25 | 2.66k | auto result = codec->startIncrementalDecode(bm.info(), bm.getPixels(), bm.rowBytes()); |
26 | 2.66k | if (result != SkCodec::kSuccess) { |
27 | 810 | return false; |
28 | 810 | } |
29 | | |
30 | | // Deliberately uninitialized to verify that incrementalDecode initializes it when it |
31 | | // returns kIncompleteInput or kErrorInInput. |
32 | 1.85k | int rowsDecoded; |
33 | 1.85k | result = codec->incrementalDecode(&rowsDecoded); |
34 | 1.85k | switch (result) { |
35 | 1.41k | case SkCodec::kIncompleteInput: |
36 | 1.68k | case SkCodec::kErrorInInput: |
37 | 1.68k | if (rowsDecoded < bm.height()) { |
38 | 1.08k | void* dst = SkTAddOffset<void>(bm.getPixels(), rowsDecoded * bm.rowBytes()); |
39 | 1.08k | sk_bzero(dst, (bm.height() - rowsDecoded) * bm.rowBytes()); |
40 | 1.08k | } |
41 | 1.68k | return true; // decoded a partial image |
42 | 168 | case SkCodec::kSuccess: |
43 | 168 | return true; |
44 | 0 | default: |
45 | 0 | return false; |
46 | 1.85k | } |
47 | 1.85k | } |
48 | | |
49 | | #if defined(SK_BUILD_FOR_LIBFUZZER) |
50 | 162k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
51 | 162k | if (size > 10240) { |
52 | 207 | return 0; |
53 | 207 | } |
54 | 162k | FuzzIncrementalImageDecode(data, size); |
55 | 162k | return 0; |
56 | 162k | } |
57 | | #endif |