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

Side by Side Diff: src/core/SkValidatingReadBuffer.cpp

Issue 61913002: Adding error checks to SkRBuffer (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fixed serialization 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 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkErrorInternals.h" 9 #include "SkErrorInternals.h"
10 #include "SkValidatingReadBuffer.h" 10 #include "SkValidatingReadBuffer.h"
11 #include "SkStream.h" 11 #include "SkStream.h"
12 #include "SkTypeface.h" 12 #include "SkTypeface.h"
13 13
14 SkValidatingReadBuffer::SkValidatingReadBuffer(const void* data, size_t size) : 14 SkValidatingReadBuffer::SkValidatingReadBuffer(const void* data, size_t size) :
15 fError(false) { 15 fError(false) {
16 this->setMemory(data, size); 16 this->setMemory(data, size);
17 this->setFlags(SkFlattenableReadBuffer::kValidation_Flag); 17 this->setFlags(SkFlattenableReadBuffer::kValidation_Flag);
18 } 18 }
19 19
20 SkValidatingReadBuffer::~SkValidatingReadBuffer() { 20 SkValidatingReadBuffer::~SkValidatingReadBuffer() {
21 } 21 }
22 22
23 void SkValidatingReadBuffer::validate(bool isValid) { 23 bool SkValidatingReadBuffer::validate(bool isValid) {
24 if (!fError && !isValid) { 24 if (!fError && !isValid) {
25 // When an error is found, send the read cursor to the end of the stream 25 // When an error is found, send the read cursor to the end of the stream
26 fReader.skip(fReader.available()); 26 fReader.skip(fReader.available());
27 fError = true; 27 fError = true;
28 } 28 }
29 return !fError;
sugoi1 2013/11/08 16:20:06 Added this to get a way to check the validity of a
29 } 30 }
30 31
31 void SkValidatingReadBuffer::setMemory(const void* data, size_t size) { 32 void SkValidatingReadBuffer::setMemory(const void* data, size_t size) {
32 this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size)); 33 this->validate(IsPtrAlign4(data) && (SkAlign4(size) == size));
33 if (!fError) { 34 if (!fError) {
34 fReader.setMemory(data, size); 35 fReader.setMemory(data, size);
35 } 36 }
36 } 37 }
37 38
38 const void* SkValidatingReadBuffer::skip(size_t size) { 39 const void* SkValidatingReadBuffer::skip(size_t size) {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 115
115 void SkValidatingReadBuffer::readPoint(SkPoint* point) { 116 void SkValidatingReadBuffer::readPoint(SkPoint* point) {
116 point->fX = fReader.readScalar(); 117 point->fX = fReader.readScalar();
117 point->fY = fReader.readScalar(); 118 point->fY = fReader.readScalar();
118 } 119 }
119 120
120 void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) { 121 void SkValidatingReadBuffer::readMatrix(SkMatrix* matrix) {
121 size_t size = 0; 122 size_t size = 0;
122 if (!fError) { 123 if (!fError) {
123 size = matrix->readFromMemory(fReader.peek(), fReader.available()); 124 size = matrix->readFromMemory(fReader.peek(), fReader.available());
124 this->validate((SkAlign4(size) != size) || (0 == size)); 125 this->validate((SkAlign4(size) == size) && (0 != size));
sugoi1 2013/11/08 16:20:06 These conditions were upside down !
125 } 126 }
126 if (!fError) { 127 if (!fError) {
127 (void)this->skip(size); 128 (void)this->skip(size);
128 } 129 }
129 } 130 }
130 131
131 void SkValidatingReadBuffer::readIRect(SkIRect* rect) { 132 void SkValidatingReadBuffer::readIRect(SkIRect* rect) {
132 const void* ptr = this->skip(sizeof(SkIRect)); 133 const void* ptr = this->skip(sizeof(SkIRect));
133 if (!fError) { 134 if (!fError) {
134 memcpy(rect, ptr, sizeof(SkIRect)); 135 memcpy(rect, ptr, sizeof(SkIRect));
135 } 136 }
136 } 137 }
137 138
138 void SkValidatingReadBuffer::readRect(SkRect* rect) { 139 void SkValidatingReadBuffer::readRect(SkRect* rect) {
139 const void* ptr = this->skip(sizeof(SkRect)); 140 const void* ptr = this->skip(sizeof(SkRect));
140 if (!fError) { 141 if (!fError) {
141 memcpy(rect, ptr, sizeof(SkRect)); 142 memcpy(rect, ptr, sizeof(SkRect));
142 } 143 }
143 } 144 }
144 145
145 void SkValidatingReadBuffer::readRegion(SkRegion* region) { 146 void SkValidatingReadBuffer::readRegion(SkRegion* region) {
146 size_t size = 0; 147 size_t size = 0;
147 if (!fError) { 148 if (!fError) {
148 size = region->readFromMemory(fReader.peek(), fReader.available()); 149 size = region->readFromMemory(fReader.peek(), fReader.available());
149 this->validate((SkAlign4(size) != size) || (0 == size)); 150 this->validate((SkAlign4(size) == size) && (0 != size));
150 } 151 }
151 if (!fError) { 152 if (!fError) {
152 (void)this->skip(size); 153 (void)this->skip(size);
153 } 154 }
154 } 155 }
155 156
156 void SkValidatingReadBuffer::readPath(SkPath* path) { 157 void SkValidatingReadBuffer::readPath(SkPath* path) {
157 size_t size = 0; 158 size_t size = 0;
158 if (!fError) { 159 if (!fError) {
159 size = path->readFromMemory(fReader.peek(), fReader.available()); 160 size = path->readFromMemory(fReader.peek(), fReader.available());
160 this->validate((SkAlign4(size) != size) || (0 == size)); 161 this->validate((SkAlign4(size) == size) && (0 != size));
161 } 162 }
162 if (!fError) { 163 if (!fError) {
163 (void)this->skip(size); 164 (void)this->skip(size);
164 } 165 }
165 } 166 }
166 167
167 bool SkValidatingReadBuffer::readArray(void* value, size_t size, size_t elementS ize) { 168 bool SkValidatingReadBuffer::readArray(void* value, size_t size, size_t elementS ize) {
168 const uint32_t count = this->getArrayCount(); 169 const uint32_t count = this->getArrayCount();
169 this->validate(size == count); 170 this->validate(size == count);
170 (void)this->skip(sizeof(uint32_t)); // Skip array count 171 (void)this->skip(sizeof(uint32_t)); // Skip array count
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 delete obj; 256 delete obj;
256 obj = NULL; 257 obj = NULL;
257 } 258 }
258 } else { 259 } else {
259 // we must skip the remaining data 260 // we must skip the remaining data
260 this->skip(sizeRecorded); 261 this->skip(sizeRecorded);
261 SkASSERT(false); 262 SkASSERT(false);
262 } 263 }
263 return obj; 264 return obj;
264 } 265 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698