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

Side by Side Diff: third_party/WebKit/Source/modules/push_messaging/PushMessageData.cpp

Issue 1414553002: Fix out-of-memory crashes related to ArrayBuffer allocation Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "modules/push_messaging/PushMessageData.h" 6 #include "modules/push_messaging/PushMessageData.h"
7 7
8 #include "bindings/core/v8/ExceptionState.h" 8 #include "bindings/core/v8/ExceptionState.h"
9 #include "bindings/core/v8/ScriptState.h" 9 #include "bindings/core/v8/ScriptState.h"
10 #include "bindings/core/v8/V8Binding.h" 10 #include "bindings/core/v8/V8Binding.h"
11 #include "bindings/modules/v8/UnionTypesModules.h" 11 #include "bindings/modules/v8/UnionTypesModules.h"
12 #include "core/dom/DOMArrayBuffer.h" 12 #include "core/dom/DOMArrayBuffer.h"
13 #include "core/fileapi/Blob.h" 13 #include "core/fileapi/Blob.h"
14 #include "platform/blob/BlobData.h" 14 #include "platform/blob/BlobData.h"
15 #include "wtf/text/TextEncoding.h" 15 #include "wtf/text/TextEncoding.h"
16 #include <v8.h> 16 #include <v8.h>
17 17
18 namespace blink { 18 namespace blink {
19 19
20 PushMessageData* PushMessageData::create(const String& messageString) 20 PushMessageData* PushMessageData::create(const String& messageString, ExceptionS tate& exceptionState)
21 { 21 {
22 return PushMessageData::create(ArrayBufferOrArrayBufferViewOrUSVString::from USVString(messageString)); 22 return PushMessageData::create(ArrayBufferOrArrayBufferViewOrUSVString::from USVString(messageString), exceptionState);
23 } 23 }
24 24
25 PushMessageData* PushMessageData::create(const ArrayBufferOrArrayBufferViewOrUSV String& messageData) 25 PushMessageData* PushMessageData::create(const ArrayBufferOrArrayBufferViewOrUSV String& messageData, ExceptionState& exceptionState)
26 { 26 {
27 if (messageData.isArrayBuffer() || messageData.isArrayBufferView()) { 27 if (messageData.isArrayBuffer() || messageData.isArrayBufferView()) {
28 RefPtr<DOMArrayBuffer> buffer = messageData.isArrayBufferView() 28 RefPtr<DOMArrayBuffer> buffer = messageData.isArrayBufferView()
29 ? messageData.getAsArrayBufferView()->buffer() 29 ? messageData.getAsArrayBufferView()->bufferOrNull()
30 : messageData.getAsArrayBuffer(); 30 : messageData.getAsArrayBuffer();
31 31
32 if (!buffer) {
33 exceptionState.throwRangeError("Out of memory. Failed to allocate ar ray buffer for push message data.");
34 return nullptr;
35 }
32 return new PushMessageData(static_cast<const char*>(buffer->data()), buf fer->byteLength()); 36 return new PushMessageData(static_cast<const char*>(buffer->data()), buf fer->byteLength());
33 } 37 }
34 38
35 if (messageData.isUSVString()) { 39 if (messageData.isUSVString()) {
36 CString encodedString = UTF8Encoding().normalizeAndEncode(messageData.ge tAsUSVString(), WTF::EntitiesForUnencodables); 40 CString encodedString = UTF8Encoding().normalizeAndEncode(messageData.ge tAsUSVString(), WTF::EntitiesForUnencodables);
37 return new PushMessageData(encodedString.data(), encodedString.length()) ; 41 return new PushMessageData(encodedString.data(), encodedString.length()) ;
38 } 42 }
39 43
40 ASSERT(messageData.isNull()); 44 ASSERT(messageData.isNull());
41 return new PushMessageData(); 45 return new PushMessageData();
42 } 46 }
43 47
44 PushMessageData::PushMessageData() 48 PushMessageData::PushMessageData()
45 { 49 {
46 } 50 }
47 51
48 PushMessageData::PushMessageData(const char* data, unsigned bytesSize) 52 PushMessageData::PushMessageData(const char* data, unsigned bytesSize)
49 { 53 {
50 m_data.append(data, bytesSize); 54 m_data.append(data, bytesSize);
51 } 55 }
52 56
53 PushMessageData::~PushMessageData() 57 PushMessageData::~PushMessageData()
54 { 58 {
55 } 59 }
56 60
57 PassRefPtr<DOMArrayBuffer> PushMessageData::arrayBuffer() const 61 PassRefPtr<DOMArrayBuffer> PushMessageData::arrayBuffer(ExceptionState& exceptio nState) const
58 { 62 {
59 return DOMArrayBuffer::create(m_data.data(), m_data.size()); 63 RefPtr<DOMArrayBuffer> buffer = DOMArrayBuffer::createOrNull(m_data.data(), m_data.size());
64 if (!buffer)
65 exceptionState.throwRangeError("Out of memory. Failed to allocate array buffer.");
66 return buffer.release();
60 } 67 }
61 68
62 Blob* PushMessageData::blob() const 69 Blob* PushMessageData::blob() const
63 { 70 {
64 OwnPtr<BlobData> blobData = BlobData::create(); 71 OwnPtr<BlobData> blobData = BlobData::create();
65 blobData->appendBytes(m_data.data(), m_data.size()); 72 blobData->appendBytes(m_data.data(), m_data.size());
66 73
67 // Note that the content type of the Blob object is deliberately not being 74 // Note that the content type of the Blob object is deliberately not being
68 // provided, following the specification. 75 // provided, following the specification.
69 76
(...skipping 21 matching lines...) Expand all
91 String PushMessageData::text() const 98 String PushMessageData::text() const
92 { 99 {
93 return UTF8Encoding().decode(m_data.data(), m_data.size()); 100 return UTF8Encoding().decode(m_data.data(), m_data.size());
94 } 101 }
95 102
96 DEFINE_TRACE(PushMessageData) 103 DEFINE_TRACE(PushMessageData)
97 { 104 {
98 } 105 }
99 106
100 } // namespace blink 107 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698