aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/InvalidIndexedPngTest.cpp
diff options
context:
space:
mode:
authorGravatar dml <dml@google.com>2015-03-11 11:12:54 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-03-11 11:12:54 -0700
commitf45796ee0eb46c2d08a6d1547622043892c525eb (patch)
tree966cd8f7ca47977c9c05f6f23c74520f6dff9e8d /tests/InvalidIndexedPngTest.cpp
parent180aec43451dd951fdaae81a92efc710ba093260 (diff)
Fix a memory leak when decoding corrupted indexed PNGs.
Diffstat (limited to 'tests/InvalidIndexedPngTest.cpp')
-rw-r--r--tests/InvalidIndexedPngTest.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/InvalidIndexedPngTest.cpp b/tests/InvalidIndexedPngTest.cpp
new file mode 100644
index 0000000000..b3789e3f6b
--- /dev/null
+++ b/tests/InvalidIndexedPngTest.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+// The PNG decoder is not available on all platforms.
+#if !defined(SK_BUILD_FOR_MAC) && \
+ !defined(SK_BUILD_FOR_WIN) && \
+ !defined(SK_BUILD_FOR_IOS)
+
+#include "SkBitmap.h"
+#include "SkForceLinking.h"
+#include "SkImageDecoder.h"
+#include "Test.h"
+
+// A valid 1x1 indexed PNG.
+unsigned char gPngData[] = {
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
+ 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
+ 0x08, 0x03, 0x00, 0x00, 0x00, 0x28, 0xcb, 0x34, 0xbb, 0x00, 0x00, 0x00,
+ 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
+ 0x1c, 0x00, 0x0f, 0x01, 0xb9, 0x8f, 0x00, 0x00, 0x00, 0x06, 0x50, 0x4c,
+ 0x54, 0x45, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0xd2, 0x87, 0xef, 0x71,
+ 0x00, 0x00, 0x00, 0x13, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0xed, 0xfd,
+ 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xf8, 0xaf, 0x16, 0x46, 0x00,
+ 0x02, 0x00, 0x01, 0x32, 0x60, 0xf7, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x49,
+ 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
+};
+
+// Attempt to decode an invalid PNG that has a palette. Mostly we're looking to
+// make sure we don't leak memory since libpng uses setjmp for error handling so
+// it's very easy to accidentally skip destructors when a failure happens.
+DEF_TEST(InvalidIndexedPng, reporter) {
+ SkBitmap image;
+ SkForceLinking(false);
+ // Make our PNG invalid by changing a byte.
+ gPngData[sizeof(gPngData) - 1] = 1;
+ bool success = SkImageDecoder::DecodeMemory(
+ gPngData, sizeof(gPngData), &image, SkColorType::kUnknown_SkColorType,
+ SkImageDecoder::kDecodePixels_Mode);
+ REPORTER_ASSERT(reporter, !success);
+}
+
+#endif // !(SK_BUILD_FOR_MAC||SK_BUILD_FOR_WIN||SK_BUILD_FOR_IOS)