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

Side by Side 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: Adding validation before memory allocation in SkRegion::readFromMemory Created 7 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkBuffer.h" 10 #include "SkBuffer.h"
(...skipping 2048 matching lines...) Expand 10 before | Expand all | Expand 10 after
2059 fPts = srcPts; 2059 fPts = srcPts;
2060 return (Verb)verb; 2060 return (Verb)verb;
2061 } 2061 }
2062 2062
2063 /////////////////////////////////////////////////////////////////////////////// 2063 ///////////////////////////////////////////////////////////////////////////////
2064 2064
2065 /* 2065 /*
2066 Format in compressed buffer: [ptCount, verbCount, pts[], verbs[]] 2066 Format in compressed buffer: [ptCount, verbCount, pts[], verbs[]]
2067 */ 2067 */
2068 2068
2069 uint32_t SkPath::writeToMemory(void* storage) const { 2069 size_t SkPath::writeToMemory(void* storage) const {
2070 SkDEBUGCODE(this->validate();) 2070 SkDEBUGCODE(this->validate();)
2071 2071
2072 if (NULL == storage) { 2072 if (NULL == storage) {
2073 const int byteCount = sizeof(int32_t) + fPathRef->writeSize(); 2073 const int byteCount = sizeof(int32_t) + fPathRef->writeSize();
2074 return SkAlign4(byteCount); 2074 return SkAlign4(byteCount);
2075 } 2075 }
2076 2076
2077 SkWBuffer buffer(storage); 2077 SkWBuffer buffer(storage);
2078 2078
2079 int32_t packed = ((fIsOval & 1) << kIsOval_SerializationShift) | 2079 int32_t packed = ((fIsOval & 1) << kIsOval_SerializationShift) |
2080 (fConvexity << kConvexity_SerializationShift) | 2080 (fConvexity << kConvexity_SerializationShift) |
2081 (fFillType << kFillType_SerializationShift) | 2081 (fFillType << kFillType_SerializationShift) |
2082 (fSegmentMask << kSegmentMask_SerializationShift) | 2082 (fSegmentMask << kSegmentMask_SerializationShift) |
2083 (fDirection << kDirection_SerializationShift) 2083 (fDirection << kDirection_SerializationShift)
2084 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O 2084 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O
2085 | (0x1 << kNewFormat_SerializationShift); 2085 | (0x1 << kNewFormat_SerializationShift);
2086 #endif 2086 #endif
2087 2087
2088 buffer.write32(packed); 2088 buffer.write32(packed);
2089 2089
2090 fPathRef->writeToBuffer(&buffer); 2090 fPathRef->writeToBuffer(&buffer);
2091 2091
2092 buffer.padToAlign4(); 2092 buffer.padToAlign4();
2093 return SkToU32(buffer.pos()); 2093 return buffer.pos();
2094 } 2094 }
2095 2095
2096 uint32_t SkPath::readFromMemory(const void* storage) { 2096 size_t SkPath::readFromMemory(const void* storage, size_t length) {
2097 SkRBuffer buffer(storage); 2097 SkRBufferWithSizeCheck buffer(storage, length);
2098 2098
2099 uint32_t packed = buffer.readS32(); 2099 uint32_t packed = buffer.readS32();
2100 fIsOval = (packed >> kIsOval_SerializationShift) & 1; 2100 fIsOval = (packed >> kIsOval_SerializationShift) & 1;
2101 fConvexity = (packed >> kConvexity_SerializationShift) & 0xFF; 2101 fConvexity = (packed >> kConvexity_SerializationShift) & 0xFF;
2102 fFillType = (packed >> kFillType_SerializationShift) & 0xFF; 2102 fFillType = (packed >> kFillType_SerializationShift) & 0xFF;
2103 fSegmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; 2103 fSegmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
2104 fDirection = (packed >> kDirection_SerializationShift) & 0x3; 2104 fDirection = (packed >> kDirection_SerializationShift) & 0x3;
2105 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O 2105 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O
2106 bool newFormat = (packed >> kNewFormat_SerializationShift) & 1; 2106 bool newFormat = (packed >> kNewFormat_SerializationShift) & 1;
2107 #endif 2107 #endif
2108 2108
2109 fPathRef.reset(SkPathRef::CreateFromBuffer(&buffer 2109 fPathRef.reset(SkPathRef::CreateFromBuffer(&buffer
2110 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O 2110 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O
2111 , newFormat, packed) 2111 , newFormat, packed
2112 #endif 2112 #endif
2113 ); 2113 ));
2114 2114
2115 buffer.skipToAlign4(); 2115 buffer.skipToAlign4();
2116 2116
2117 SkDEBUGCODE(this->validate();) 2117 size_t sizeRead = 0;
2118 return SkToU32(buffer.pos()); 2118 if (buffer.isValid()) {
2119 SkDEBUGCODE(this->validate();)
2120 sizeRead = buffer.pos();
2121 }
2122 return sizeRead;
2119 } 2123 }
2120 2124
2121 /////////////////////////////////////////////////////////////////////////////// 2125 ///////////////////////////////////////////////////////////////////////////////
2122 2126
2123 #include "SkString.h" 2127 #include "SkString.h"
2124 2128
2125 static void append_scalar(SkString* str, SkScalar value) { 2129 static void append_scalar(SkString* str, SkScalar value) {
2126 SkString tmp; 2130 SkString tmp;
2127 tmp.printf("%g", value); 2131 tmp.printf("%g", value);
2128 if (tmp.contains('.')) { 2132 if (tmp.contains('.')) {
(...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after
3002 switch (this->getFillType()) { 3006 switch (this->getFillType()) {
3003 case SkPath::kEvenOdd_FillType: 3007 case SkPath::kEvenOdd_FillType:
3004 case SkPath::kInverseEvenOdd_FillType: 3008 case SkPath::kInverseEvenOdd_FillType:
3005 w &= 1; 3009 w &= 1;
3006 break; 3010 break;
3007 default: 3011 default:
3008 break; 3012 break;
3009 } 3013 }
3010 return SkToBool(w); 3014 return SkToBool(w);
3011 } 3015 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698