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

Side by Side Diff: third_party/WebKit/Source/core/html/ImageData.cpp

Issue 2763613003: Fix signed integer overflow in ImageData (Closed)
Patch Set: Addressing comments Created 3 years, 9 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 } 115 }
116 116
117 if ((paramFlags & kParamHeight & kParamWidth) && 117 if ((paramFlags & kParamHeight & kParamWidth) &&
118 height != dataLength / (4 * width)) 118 height != dataLength / (4 * width))
119 return RaiseDOMExceptionAndReturnFalse( 119 return RaiseDOMExceptionAndReturnFalse(
120 exceptionState, IndexSizeError, 120 exceptionState, IndexSizeError,
121 "The input data length is not equal to (4 * width * height)."); 121 "The input data length is not equal to (4 * width * height).");
122 } 122 }
123 123
124 if (paramFlags & kParamSize) { 124 if (paramFlags & kParamSize) {
125 if (!size->width() || !size->height()) 125 if (size->width() <= 0 || size->height() <= 0)
126 return false; 126 return false;
127 CheckedNumeric<unsigned> dataSize = 4; 127 CheckedNumeric<unsigned> dataSize = 4;
128 dataSize *= size->width(); 128 dataSize *= size->width();
129 dataSize *= size->height(); 129 dataSize *= size->height();
130 if (!dataSize.IsValid()) 130 if (!dataSize.IsValid())
131 return false; 131 return false;
132 if (paramFlags & kParamData) { 132 if (paramFlags & kParamData) {
133 if (dataSize.ValueOrDie() > dataLength) 133 if (dataSize.ValueOrDie() > dataLength)
134 return false; 134 return false;
135 } 135 }
136 } 136 }
137 137
138 return true; 138 return true;
139 } 139 }
140 140
141 DOMArrayBufferView* ImageData::allocateAndValidateDataArray( 141 DOMArrayBufferView* ImageData::allocateAndValidateDataArray(
142 const unsigned& length, 142 const unsigned& length,
143 ImageDataStorageFormat storageFormat, 143 ImageDataStorageFormat storageFormat,
144 ExceptionState* exceptionState) { 144 ExceptionState* exceptionState) {
145 if (!length) 145 if (!length)
146 return nullptr; 146 return nullptr;
147 147
148 DOMArrayBufferView* dataArray = nullptr; 148 DOMArrayBufferView* dataArray = nullptr;
149 unsigned dataLength = 0; 149 unsigned dataLength = 0;
150 unsigned dataItemLength = 1;
150 switch (storageFormat) { 151 switch (storageFormat) {
151 case kUint8ClampedArrayStorageFormat: 152 case kUint8ClampedArrayStorageFormat:
152 dataArray = DOMUint8ClampedArray::createOrNull(length); 153 dataArray = DOMUint8ClampedArray::createOrNull(length);
153 dataLength = dataArray->view()->byteLength();
154 break; 154 break;
155 case kUint16ArrayStorageFormat: 155 case kUint16ArrayStorageFormat:
156 dataArray = DOMUint16Array::createOrNull(length); 156 dataArray = DOMUint16Array::createOrNull(length);
157 dataLength = dataArray->view()->byteLength() / 2; 157 dataItemLength = 2;
158 break; 158 break;
159 case kFloat32ArrayStorageFormat: 159 case kFloat32ArrayStorageFormat:
160 dataArray = DOMFloat32Array::createOrNull(length); 160 dataArray = DOMFloat32Array::createOrNull(length);
161 dataLength = dataArray->view()->byteLength() / 4; 161 dataItemLength = 4;
162 break; 162 break;
163 default: 163 default:
164 NOTREACHED(); 164 NOTREACHED();
165 } 165 }
166 166
167 if (dataArray)
168 dataLength = dataArray->view()->byteLength() / dataItemLength;
169
167 if (!dataArray || length != dataLength) { 170 if (!dataArray || length != dataLength) {
168 if (exceptionState) 171 if (exceptionState)
169 exceptionState->throwDOMException(V8RangeError, 172 exceptionState->throwDOMException(V8RangeError,
170 "Out of memory at ImageData creation"); 173 "Out of memory at ImageData creation");
171 return nullptr; 174 return nullptr;
172 } 175 }
173 176
174 return dataArray; 177 return dataArray;
175 } 178 }
176 179
177 ImageData* ImageData::create(const IntSize& size) { 180 ImageData* ImageData::create(const IntSize& size) {
178 if (!ImageData::validateConstructorArguments(kParamSize, &size)) 181 if (!ImageData::validateConstructorArguments(kParamSize, &size))
179 return nullptr; 182 return nullptr;
180 DOMArrayBufferView* byteArray = allocateAndValidateDataArray( 183 DOMArrayBufferView* byteArray =
181 4 * size.width() * size.height(), kUint8ClampedArrayStorageFormat); 184 allocateAndValidateDataArray(4 * static_cast<unsigned>(size.width()) *
182 return new ImageData(size, byteArray); 185 static_cast<unsigned>(size.height()),
186 kUint8ClampedArrayStorageFormat);
187 return byteArray ? new ImageData(size, byteArray) : nullptr;
183 } 188 }
184 189
185 // This function accepts size (0, 0) and always returns the ImageData in 190 // This function accepts size (0, 0) and always returns the ImageData in
186 // "srgb" color space and "uint8" storage format. 191 // "srgb" color space and "uint8" storage format.
187 ImageData* ImageData::createForTest(const IntSize& size) { 192 ImageData* ImageData::createForTest(const IntSize& size) {
188 CheckedNumeric<unsigned> dataSize = 4; 193 CheckedNumeric<unsigned> dataSize = 4;
189 dataSize *= size.width(); 194 dataSize *= size.width();
190 dataSize *= size.height(); 195 dataSize *= size.height();
191 if (!dataSize.IsValid()) 196 if (!dataSize.IsValid())
192 return nullptr; 197 return nullptr;
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 SECURITY_CHECK(static_cast<unsigned>(size.width() * size.height() * 4) <= 448 SECURITY_CHECK(static_cast<unsigned>(size.width() * size.height() * 4) <=
444 m_dataF32->length()); 449 m_dataF32->length());
445 break; 450 break;
446 451
447 default: 452 default:
448 NOTREACHED(); 453 NOTREACHED();
449 } 454 }
450 } 455 }
451 456
452 } // namespace blink 457 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/BUILD.gn ('k') | third_party/WebKit/Source/core/html/ImageDataTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698