| 1 |
/* |
| 2 |
* Copyright 2025 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/core/SkBitmap.h" |
| 9 |
#include "include/core/SkData.h" |
| 10 |
#include "include/core/SkStream.h" |
| 11 |
|
| 12 |
#include "experimental/rust_bmp/decoder/SkBmpRustDecoder.h" |
| 13 |
|
| 14 |
bool FuzzBMPRustDecoder(const uint8_t* data, size_t size) { |
| 15 |
if (size == 0) { |
| 16 |
return false; |
| 17 |
} |
| 18 |
|
| 19 |
auto stream = std::make_unique<SkMemoryStream>(data, size, false); |
| 20 |
SkCodec::Result result; |
| 21 |
std::unique_ptr<SkCodec> codec = SkBmpRustDecoder::Decode(std::move(stream), &result); |
| 22 |
|
| 23 |
if (!codec || result != SkCodec::kSuccess) { |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
SkImageInfo info = codec->getInfo(); |
| 28 |
SkBitmap bitmap; |
| 29 |
if (!bitmap.tryAllocPixels(info)) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
// Attempt to decode the image |
| 34 |
(void)codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes()); |
| 35 |
|
| 36 |
// We don't care if the decode succeeds or fails - we just want to make sure it doesn't crash |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
#if defined(SK_BUILD_FOR_LIBFUZZER) |
| 41 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 42 |
// Limit input size to prevent timeouts |
| 43 |
if (size > 10240) { |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
FuzzBMPRustDecoder(data, size); |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
#endif |