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

Unified Diff: src/core/SkPath.cpp

Issue 41253002: Checking structure sizes before reading them from memory to avoid overflowing the buffer's stream. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 2 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
Index: src/core/SkPath.cpp
diff --git a/src/core/SkPath.cpp b/src/core/SkPath.cpp
index 9df62850fd2eb5bc9cf442dc4675b7091cded8c1..c765823ac5ac3c3c725a0303861c626e2b710049 100644
--- a/src/core/SkPath.cpp
+++ b/src/core/SkPath.cpp
@@ -9,6 +9,7 @@
#include "SkBuffer.h"
#include "SkErrorInternals.h"
+#include "SkFlattenableBuffers.h"
#include "SkMath.h"
#include "SkPath.h"
#include "SkPathRef.h"
@@ -2086,12 +2087,28 @@ SkPath::Verb SkPath::RawIter::next(SkPoint pts[4]) {
Format in compressed buffer: [ptCount, verbCount, pts[], verbs[]]
*/
+uint32_t SkPath::sizeInMemory() const {
+ const int byteCount = sizeof(int32_t) + fPathRef->writeSize();
+ return SkAlign4(byteCount);
+}
+
+uint32_t SkPath::SizeToRead(SkFlattenableReadBuffer& buffer) {
+#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO
+ uint32_t packed = buffer.getArrayCount();
+ bool newFormat = (packed >> kNewFormat_SerializationShift) & 1;
+#endif
+ return sizeof(int32_t) + SkPathRef::SizeToRead(buffer
+#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO
+ , newFormat
+#endif
+ );
+}
+
uint32_t SkPath::writeToMemory(void* storage) const {
SkDEBUGCODE(this->validate();)
if (NULL == storage) {
- const int byteCount = sizeof(int32_t) + fPathRef->writeSize();
- return SkAlign4(byteCount);
+ return sizeInMemory();
}
SkWBuffer buffer(storage);
@@ -2110,7 +2127,9 @@ uint32_t SkPath::writeToMemory(void* storage) const {
fPathRef->writeToBuffer(&buffer);
buffer.padToAlign4();
- return SkToU32(buffer.pos());
+ uint32_t writeSize = SkToU32(buffer.pos());
+ SkASSERT(sizeInMemory() == writeSize);
+ return writeSize;
}
uint32_t SkPath::readFromMemory(const void* storage) {
@@ -2128,16 +2147,18 @@ uint32_t SkPath::readFromMemory(const void* storage) {
fPathRef.reset(SkPathRef::CreateFromBuffer(&buffer
#ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TOO
- , newFormat, packed)
+ , newFormat, packed
#endif
- );
+ ));
buffer.skipToAlign4();
GEN_ID_INC;
SkDEBUGCODE(this->validate();)
- return SkToU32(buffer.pos());
+ uint32_t readSize = SkToU32(buffer.pos());
+ SkASSERT(sizeInMemory() == readSize);
+ return readSize;
}
///////////////////////////////////////////////////////////////////////////////

Powered by Google App Engine
This is Rietveld 408576698