Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1605)

Unified Diff: tests/CodexTest.cpp

Issue 1058873006: Get rid of leaks in SkCodec::NewFromStream. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add comment that ReadHeader does not own SkStream. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/codec/SkCodec_wbmp.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/CodexTest.cpp
diff --git a/tests/CodexTest.cpp b/tests/CodexTest.cpp
index 825251854db333f66b879dcc12a80bd3506e6f2a..26846a4550816371281c0797781a1b90cb31c355 100644
--- a/tests/CodexTest.cpp
+++ b/tests/CodexTest.cpp
@@ -109,3 +109,32 @@ DEF_TEST(Codec, r) {
check(r, "randPixels.png", SkISize::Make(8, 8), true);
check(r, "yellow_rose.png", SkISize::Make(400, 301), true);
}
+
+static void test_invalid_stream(skiatest::Reporter* r, const void* stream, size_t len) {
+ SkCodec* codec = SkCodec::NewFromStream(new SkMemoryStream(stream, len, false));
+ // We should not have gotten a codec. Bots should catch us if we leaked anything.
+ REPORTER_ASSERT(r, !codec);
+}
+
+// Ensure that SkCodec::NewFromStream handles freeing the passed in SkStream,
+// even on failure. Test some bad streams.
+DEF_TEST(Codec_leaks, r) {
+ // No codec should claim this as their format, so this tests SkCodec::NewFromStream.
+ const char nonSupportedStream[] = "hello world";
+ // The other strings should look like the beginning of a file type, so we'll call some
+ // internal version of NewFromStream, which must also delete the stream on failure.
+ const unsigned char emptyPng[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
+ const unsigned char emptyJpeg[] = { 0xFF, 0xD8, 0xFF };
+ const char emptyWebp[] = "RIFF1234WEBPVP";
+ const char emptyBmp[] = { 'B', 'M' };
+ const char emptyIco[] = { '\x00', '\x00', '\x01', '\x00' };
+ const char emptyGif[] = "GIFVER";
+
+ test_invalid_stream(r, nonSupportedStream, sizeof(nonSupportedStream));
+ test_invalid_stream(r, emptyPng, sizeof(emptyPng));
+ test_invalid_stream(r, emptyJpeg, sizeof(emptyJpeg));
+ test_invalid_stream(r, emptyWebp, sizeof(emptyWebp));
+ test_invalid_stream(r, emptyBmp, sizeof(emptyBmp));
+ test_invalid_stream(r, emptyIco, sizeof(emptyIco));
+ test_invalid_stream(r, emptyGif, sizeof(emptyGif));
+}
« no previous file with comments | « src/codec/SkCodec_wbmp.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698