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

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: Fixed comments and added tests 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 2068 matching lines...) Expand 10 before | Expand all | Expand 10 after
2079 fPts = srcPts; 2079 fPts = srcPts;
2080 return (Verb)verb; 2080 return (Verb)verb;
2081 } 2081 }
2082 2082
2083 /////////////////////////////////////////////////////////////////////////////// 2083 ///////////////////////////////////////////////////////////////////////////////
2084 2084
2085 /* 2085 /*
2086 Format in compressed buffer: [ptCount, verbCount, pts[], verbs[]] 2086 Format in compressed buffer: [ptCount, verbCount, pts[], verbs[]]
2087 */ 2087 */
2088 2088
2089 uint32_t SkPath::writeToMemory(void* storage) const { 2089 size_t SkPath::writeToMemory(void* storage) const {
2090 SkDEBUGCODE(this->validate();) 2090 SkDEBUGCODE(this->validate();)
2091 2091
2092 if (NULL == storage) { 2092 if (NULL == storage) {
2093 const int byteCount = sizeof(int32_t) + fPathRef->writeSize(); 2093 const int byteCount = sizeof(int32_t) + fPathRef->writeSize();
2094 return SkAlign4(byteCount); 2094 return SkAlign4(byteCount);
2095 } 2095 }
2096 2096
2097 SkWBuffer buffer(storage); 2097 SkWBuffer buffer(storage);
2098 2098
2099 int32_t packed = ((fIsOval & 1) << kIsOval_SerializationShift) | 2099 int32_t packed = ((fIsOval & 1) << kIsOval_SerializationShift) |
2100 (fConvexity << kConvexity_SerializationShift) | 2100 (fConvexity << kConvexity_SerializationShift) |
2101 (fFillType << kFillType_SerializationShift) | 2101 (fFillType << kFillType_SerializationShift) |
2102 (fSegmentMask << kSegmentMask_SerializationShift) | 2102 (fSegmentMask << kSegmentMask_SerializationShift) |
2103 (fDirection << kDirection_SerializationShift) 2103 (fDirection << kDirection_SerializationShift)
2104 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O 2104 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O
2105 | (0x1 << kNewFormat_SerializationShift); 2105 | (0x1 << kNewFormat_SerializationShift);
2106 #endif 2106 #endif
2107 2107
2108 buffer.write32(packed); 2108 buffer.write32(packed);
2109 2109
2110 fPathRef->writeToBuffer(&buffer); 2110 fPathRef->writeToBuffer(&buffer);
2111 2111
2112 buffer.padToAlign4(); 2112 buffer.padToAlign4();
2113 return SkToU32(buffer.pos()); 2113 return buffer.pos();
2114 } 2114 }
2115 2115
2116 uint32_t SkPath::readFromMemory(const void* storage) { 2116 size_t SkPath::readFromMemory(const void* storage, size_t length) {
2117 SkRBuffer buffer(storage); 2117 SkRBufferWithSizeCheck buffer(storage, length);
2118 2118
2119 uint32_t packed = buffer.readS32(); 2119 uint32_t packed = buffer.readS32();
2120 fIsOval = (packed >> kIsOval_SerializationShift) & 1; 2120 fIsOval = (packed >> kIsOval_SerializationShift) & 1;
2121 fConvexity = (packed >> kConvexity_SerializationShift) & 0xFF; 2121 fConvexity = (packed >> kConvexity_SerializationShift) & 0xFF;
2122 fFillType = (packed >> kFillType_SerializationShift) & 0xFF; 2122 fFillType = (packed >> kFillType_SerializationShift) & 0xFF;
2123 fSegmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF; 2123 fSegmentMask = (packed >> kSegmentMask_SerializationShift) & 0xF;
2124 fDirection = (packed >> kDirection_SerializationShift) & 0x3; 2124 fDirection = (packed >> kDirection_SerializationShift) & 0x3;
2125 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O 2125 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O
2126 bool newFormat = (packed >> kNewFormat_SerializationShift) & 1; 2126 bool newFormat = (packed >> kNewFormat_SerializationShift) & 1;
2127 #endif 2127 #endif
2128 2128
2129 fPathRef.reset(SkPathRef::CreateFromBuffer(&buffer 2129 fPathRef.reset(SkPathRef::CreateFromBuffer(&buffer
2130 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O 2130 #ifndef DELETE_THIS_CODE_WHEN_SKPS_ARE_REBUILT_AT_V14_AND_ALL_OTHER_INSTANCES_TO O
2131 , newFormat, packed) 2131 , newFormat, packed
2132 #endif 2132 #endif
2133 ); 2133 ));
2134 2134
2135 buffer.skipToAlign4(); 2135 buffer.skipToAlign4();
2136 2136
2137 GEN_ID_INC; 2137 GEN_ID_INC;
2138 2138
2139 SkDEBUGCODE(this->validate();) 2139 SkDEBUGCODE(this->validate();)
2140 return SkToU32(buffer.pos()); 2140 return buffer.isValid() ? buffer.pos() : 0;
2141 } 2141 }
2142 2142
2143 /////////////////////////////////////////////////////////////////////////////// 2143 ///////////////////////////////////////////////////////////////////////////////
2144 2144
2145 #include "SkString.h" 2145 #include "SkString.h"
2146 2146
2147 static void append_scalar(SkString* str, SkScalar value) { 2147 static void append_scalar(SkString* str, SkScalar value) {
2148 SkString tmp; 2148 SkString tmp;
2149 tmp.printf("%g", value); 2149 tmp.printf("%g", value);
2150 if (tmp.contains('.')) { 2150 if (tmp.contains('.')) {
(...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after
3002 switch (this->getFillType()) { 3002 switch (this->getFillType()) {
3003 case SkPath::kEvenOdd_FillType: 3003 case SkPath::kEvenOdd_FillType:
3004 case SkPath::kInverseEvenOdd_FillType: 3004 case SkPath::kInverseEvenOdd_FillType:
3005 w &= 1; 3005 w &= 1;
3006 break; 3006 break;
3007 default: 3007 default:
3008 break; 3008 break;
3009 } 3009 }
3010 return SkToBool(w); 3010 return SkToBool(w);
3011 } 3011 }
OLDNEW
« include/core/SkReader32.h ('K') | « src/core/SkMatrix.cpp ('k') | src/core/SkRRect.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698