diff options
author | yujieqin <yujieqin@google.com> | 2016-03-09 13:49:36 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-09 13:49:37 -0800 |
commit | c04df21bb624e299440ce8b56495ec109d4e824e (patch) | |
tree | 09002e620d651b2f9b343fa5a2cd2fa2dcd47de1 | |
parent | e344b26a7d3c0efaab21bb17fa5b2d96dfc92edf (diff) |
Limit the maximum buffer size of SkRawBufferedStream
BUG=b/27475341
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1780643003
Review URL: https://codereview.chromium.org/1780643003
-rw-r--r-- | src/codec/SkRawCodec.cpp | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp index 524a438d29..609b0aef02 100644 --- a/src/codec/SkRawCodec.cpp +++ b/src/codec/SkRawCodec.cpp @@ -199,6 +199,28 @@ public: virtual SkMemoryStream* transferBuffer(size_t offset, size_t size) = 0; }; +class SkRawLimitedDynamicMemoryWStream : public SkDynamicMemoryWStream { +public: + virtual ~SkRawLimitedDynamicMemoryWStream() {} + + bool write(const void* buffer, size_t size) override { + size_t newSize; + if (!safe_add_to_size_t(this->bytesWritten(), size, &newSize) || + newSize > kMaxStreamSize) + { + SkCodecPrintf("Error: Stream size exceeds the limit.\n"); + return false; + } + return this->INHERITED::write(buffer, size); + } + +private: + const size_t kMaxStreamSize = 100 * 1024 * 1024; // 100MB + + typedef SkDynamicMemoryWStream INHERITED; +}; + +// Note: the maximum buffer size is 100MB (limited by SkRawLimitedDynamicMemoryWStream). class SkRawBufferedStream : public SkRawStream { public: // Will take the ownership of the stream. @@ -301,7 +323,8 @@ private: SkAutoTDelete<SkStream> fStream; bool fWholeStreamRead; - SkDynamicMemoryWStream fStreamBuffer; + // Use a size-limited stream to avoid holding too huge buffer. + SkRawLimitedDynamicMemoryWStream fStreamBuffer; const size_t kReadToEnd = 0; }; |