Commit b66121f3 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[nfc] Replace uses of DeprecatedByteLengthAsUnsigned

This CL replaces calls to DeprecatedByteLengthAsUnsigned by calls to
ByteLengthAsSizeT. Unfortunately the current implementation cannot deal
with huge ArrayBuffers yet. Therefore I reject the incoming
ArrayBuffers with a RangeError if its size is too big.

Background: we prepare ArrayBuffers to be bigger than 4GB. Therefore we
changed the size field to size_t. Now we are changing all uses of
ByteLength to be able to deal with size_t, either by accepting a size_t,
or by throwing an exception if the size is too big.

R=reillyg@chromium.org

Bug: chromium:1008840
Change-Id: Ia0af7fb8c57ec522cf8cd0963d6e0dd3fad9dd92
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1973910Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#726041}
parent 87e6722b
...@@ -40,23 +40,34 @@ bool IsBufferSource(const NDEFRecordDataSource& data) { ...@@ -40,23 +40,34 @@ bool IsBufferSource(const NDEFRecordDataSource& data) {
return data.IsArrayBuffer() || data.IsArrayBufferView(); return data.IsArrayBuffer() || data.IsArrayBufferView();
} }
WTF::Vector<uint8_t> GetBytesOfBufferSource( bool GetBytesOfBufferSource(const NDEFRecordDataSource& buffer_source,
const NDEFRecordDataSource& buffer_source) { WTF::Vector<uint8_t>* target,
ExceptionState& exception_state) {
DCHECK(IsBufferSource(buffer_source)); DCHECK(IsBufferSource(buffer_source));
WTF::Vector<uint8_t> bytes; uint8_t* data;
size_t data_length;
if (buffer_source.IsArrayBuffer()) { if (buffer_source.IsArrayBuffer()) {
DOMArrayBuffer* array_buffer = buffer_source.GetAsArrayBuffer(); DOMArrayBuffer* array_buffer = buffer_source.GetAsArrayBuffer();
bytes.Append(static_cast<uint8_t*>(array_buffer->Data()), data = reinterpret_cast<uint8_t*>(array_buffer->Data());
array_buffer->DeprecatedByteLengthAsUnsigned()); data_length = array_buffer->ByteLengthAsSizeT();
} else if (buffer_source.IsArrayBufferView()) { } else if (buffer_source.IsArrayBufferView()) {
const DOMArrayBufferView* array_buffer_view = const DOMArrayBufferView* array_buffer_view =
buffer_source.GetAsArrayBufferView().View(); buffer_source.GetAsArrayBufferView().View();
bytes.Append(static_cast<uint8_t*>(array_buffer_view->BaseAddress()), data = reinterpret_cast<uint8_t*>(array_buffer_view->BaseAddress());
array_buffer_view->deprecatedByteLengthAsUnsigned()); data_length = array_buffer_view->byteLengthAsSizeT();
} else { } else {
NOTREACHED(); NOTREACHED();
return false;
}
wtf_size_t checked_length;
if (!base::CheckedNumeric<wtf_size_t>(data_length)
.AssignIfValid(&checked_length)) {
exception_state.ThrowRangeError(
"The provided buffer source exceeds the maximum supported length");
return false;
} }
return bytes; target->Append(data, checked_length);
return true;
} }
// https://w3c.github.io/web-nfc/#ndef-record-types // https://w3c.github.io/web-nfc/#ndef-record-types
...@@ -153,7 +164,9 @@ static NDEFRecord* CreateTextRecord(const ExecutionContext* execution_context, ...@@ -153,7 +164,9 @@ static NDEFRecord* CreateTextRecord(const ExecutionContext* execution_context,
bytes.Append(utf8_string.data(), utf8_string.size()); bytes.Append(utf8_string.data(), utf8_string.size());
} else { } else {
DCHECK(IsBufferSource(data)); DCHECK(IsBufferSource(data));
bytes = GetBytesOfBufferSource(data); if (!GetBytesOfBufferSource(data, &bytes, exception_state)) {
return nullptr;
}
} }
return MakeGarbageCollected<NDEFRecord>("text", encoding_label, language, return MakeGarbageCollected<NDEFRecord>("text", encoding_label, language,
...@@ -192,8 +205,11 @@ static NDEFRecord* CreateMimeRecord(const NDEFRecordDataSource& data, ...@@ -192,8 +205,11 @@ static NDEFRecord* CreateMimeRecord(const NDEFRecordDataSource& data,
return nullptr; return nullptr;
} }
return MakeGarbageCollected<NDEFRecord>(GetBytesOfBufferSource(data), WTF::Vector<uint8_t> bytes;
media_type); if (!GetBytesOfBufferSource(data, &bytes, exception_state)) {
return nullptr;
}
return MakeGarbageCollected<NDEFRecord>(bytes, media_type);
} }
static NDEFRecord* CreateUnknownRecord(const NDEFRecordDataSource& data, static NDEFRecord* CreateUnknownRecord(const NDEFRecordDataSource& data,
...@@ -204,8 +220,11 @@ static NDEFRecord* CreateUnknownRecord(const NDEFRecordDataSource& data, ...@@ -204,8 +220,11 @@ static NDEFRecord* CreateUnknownRecord(const NDEFRecordDataSource& data,
return nullptr; return nullptr;
} }
return MakeGarbageCollected<NDEFRecord>("unknown", WTF::Vector<uint8_t> bytes;
GetBytesOfBufferSource(data)); if (!GetBytesOfBufferSource(data, &bytes, exception_state)) {
return nullptr;
}
return MakeGarbageCollected<NDEFRecord>("unknown", bytes);
} }
static NDEFRecord* CreateExternalRecord(const String& custom_type, static NDEFRecord* CreateExternalRecord(const String& custom_type,
...@@ -221,8 +240,11 @@ static NDEFRecord* CreateExternalRecord(const String& custom_type, ...@@ -221,8 +240,11 @@ static NDEFRecord* CreateExternalRecord(const String& custom_type,
return nullptr; return nullptr;
} }
return MakeGarbageCollected<NDEFRecord>(custom_type, WTF::Vector<uint8_t> bytes;
GetBytesOfBufferSource(data)); if (!GetBytesOfBufferSource(data, &bytes, exception_state)) {
return nullptr;
}
return MakeGarbageCollected<NDEFRecord>(custom_type, bytes);
} }
} // namespace } // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment