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

Side by Side Diff: src/core/SkBuffer.h

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 #ifndef SkBuffer_DEFINED 10 #ifndef SkBuffer_DEFINED
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 size_t size() const { return fStop - fData; } 49 size_t size() const { return fStop - fData; }
50 /** Return true if the buffer has read to the end of the data pointer. 50 /** Return true if the buffer has read to the end of the data pointer.
51 Only defined if the length was specified in the constructor or in a call 51 Only defined if the length was specified in the constructor or in a call
52 to reset(). Always returns true if the length was not specified. 52 to reset(). Always returns true if the length was not specified.
53 */ 53 */
54 bool eof() const { return fPos >= fStop; } 54 bool eof() const { return fPos >= fStop; }
55 55
56 /** Read the specified number of bytes from the data pointer. If buffer is n ot 56 /** Read the specified number of bytes from the data pointer. If buffer is n ot
57 null, copy those bytes into buffer. 57 null, copy those bytes into buffer.
58 */ 58 */
59 void read(void* buffer, size_t size) { 59 virtual void read(void* buffer, size_t size) {
60 if (size) { 60 if (size) {
61 this->readNoSizeCheck(buffer, size); 61 this->readNoSizeCheck(buffer, size);
62 } 62 }
63 } 63 }
64 64
65 const void* skip(size_t size); // return start of skipped data 65 const void* skip(size_t size); // return start of skipped data
66 size_t skipToAlign4(); 66 size_t skipToAlign4();
67 67
68 void* readPtr() { void* ptr; read(&ptr, sizeof(ptr)); return ptr; } 68 void* readPtr() { void* ptr; read(&ptr, sizeof(ptr)); return ptr; }
69 SkScalar readScalar() { SkScalar x; read(&x, 4); return x; } 69 SkScalar readScalar() { SkScalar x; read(&x, 4); return x; }
70 uint32_t readU32() { uint32_t x; read(&x, 4); return x; } 70 uint32_t readU32() { uint32_t x; read(&x, 4); return x; }
71 int32_t readS32() { int32_t x; read(&x, 4); return x; } 71 int32_t readS32() { int32_t x; read(&x, 4); return x; }
72 uint16_t readU16() { uint16_t x; read(&x, 2); return x; } 72 uint16_t readU16() { uint16_t x; read(&x, 2); return x; }
73 int16_t readS16() { int16_t x; read(&x, 2); return x; } 73 int16_t readS16() { int16_t x; read(&x, 2); return x; }
74 uint8_t readU8() { uint8_t x; read(&x, 1); return x; } 74 uint8_t readU8() { uint8_t x; read(&x, 1); return x; }
75 bool readBool() { return this->readU8() != 0; } 75 bool readBool() { return this->readU8() != 0; }
76 76
77 private: 77 protected:
78 void readNoSizeCheck(void* buffer, size_t size); 78 void readNoSizeCheck(void* buffer, size_t size);
79 79
80 const char* fData; 80 const char* fData;
81 const char* fPos; 81 const char* fPos;
82 const char* fStop; 82 const char* fStop;
83 }; 83 };
84 84
85 /** \class SkRBufferWithSizeCheck
86
87 Same as SkRBuffer, except that a size check is performed before the read ope ration and an
88 error is set if the read operation is attempting to read past the end of the data.
89 */
90 class SkRBufferWithSizeCheck : public SkRBuffer {
91 public:
92 SkRBufferWithSizeCheck(const void* data, size_t size) : SkRBuffer(data, size ), fError(false) {}
93
94 /** Read the specified number of bytes from the data pointer. If buffer is n ot
95 null and the number of bytes to read does not overflow this object's dat a,
96 copy those bytes into buffer.
97 */
98 virtual void read(void* buffer, size_t size) SK_OVERRIDE;
99
100 /** Returns whether or not a read operation attempted to read past the end o f the data.
101 */
102 bool isValid() const { return !fError; }
103 private:
104 bool fError;
105 };
106
85 /** \class SkWBuffer 107 /** \class SkWBuffer
86 108
87 Light weight class for writing data to a memory block. 109 Light weight class for writing data to a memory block.
88 The WBuffer is given the buffer to write into, with either a specified size 110 The WBuffer is given the buffer to write into, with either a specified size
89 or no size, in which case no range checking is performed. An empty WBuffer 111 or no size, in which case no range checking is performed. An empty WBuffer
90 is legal, in which case no data is ever written, but the relative pos() 112 is legal, in which case no data is ever written, but the relative pos()
91 is updated. 113 is updated.
92 */ 114 */
93 class SkWBuffer : SkNoncopyable { 115 class SkWBuffer : SkNoncopyable {
94 public: 116 public:
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 151
130 private: 152 private:
131 void writeNoSizeCheck(const void* buffer, size_t size); 153 void writeNoSizeCheck(const void* buffer, size_t size);
132 154
133 char* fData; 155 char* fData;
134 char* fPos; 156 char* fPos;
135 char* fStop; 157 char* fStop;
136 }; 158 };
137 159
138 #endif 160 #endif
OLDNEW
« no previous file with comments | « samplecode/SampleRegion.cpp ('k') | src/core/SkBuffer.cpp » ('j') | tests/SerializationTest.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698