Commit 0fc3b3ad authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[arraybuffer] ArrayBufferContents::Data() should not return nullptr

There still seem to be issues when ArrayBufferContents::Data() returns
nullptr for now, see https://crbug.com/1028766. That's why I undo that
change which allows nullptr for now and try to fix it after the branch
cut.

R=haraken@chromium.org

Bug: chromium:1008840, chromium:1027937
Change-Id: I6c541e56ef13d2506c32add301dd92e0475f41c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1942695Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720150}
parent 175ae95a
...@@ -71,6 +71,29 @@ ArrayBufferContents::ArrayBufferContents( ...@@ -71,6 +71,29 @@ ArrayBufferContents::ArrayBufferContents(
} }
} }
ArrayBufferContents::ArrayBufferContents(
std::shared_ptr<v8::BackingStore> backing_store) {
if (!backing_store || backing_store->Data()) {
backing_store_ = std::move(backing_store);
return;
}
// ArrayBufferContents has to guarantee that Data() provides a valid pointer,
// even when DataSize() is '0'. That's why we create a new BackingStore here.
// TODO(ahaas): Remove this code here once nullptr is a valid result for
// Data().
CHECK_EQ(backing_store->ByteLength(), 0u);
void* data = AllocateMemoryOrNull(0, kDontInitialize);
CHECK_NE(data, nullptr);
DataDeleter deleter = [](void* data, size_t, void*) { FreeMemory(data); };
if (!backing_store->IsShared()) {
backing_store_ =
v8::ArrayBuffer::NewBackingStore(data, 0, deleter, nullptr);
} else {
backing_store_ =
v8::SharedArrayBuffer::NewBackingStore(data, 0, deleter, nullptr);
}
}
ArrayBufferContents::~ArrayBufferContents() = default; ArrayBufferContents::~ArrayBufferContents() = default;
void ArrayBufferContents::Detach() { void ArrayBufferContents::Detach() {
......
...@@ -65,8 +65,7 @@ class CORE_EXPORT ArrayBufferContents { ...@@ -65,8 +65,7 @@ class CORE_EXPORT ArrayBufferContents {
InitializationPolicy); InitializationPolicy);
ArrayBufferContents(void* data, size_t length, DataDeleter deleter); ArrayBufferContents(void* data, size_t length, DataDeleter deleter);
ArrayBufferContents(ArrayBufferContents&&) = default; ArrayBufferContents(ArrayBufferContents&&) = default;
explicit ArrayBufferContents(std::shared_ptr<v8::BackingStore> backing_store) explicit ArrayBufferContents(std::shared_ptr<v8::BackingStore> backing_store);
: backing_store_(std::move(backing_store)) {}
~ArrayBufferContents(); ~ArrayBufferContents();
......
<!DOCTYPE html>
<title>Verify that calling postMessage with an empty ArrayBuffer is valid.</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
test(function(t) {
const arrayBuffer = new ArrayBuffer(0);
const { port1 } = new MessageChannel();
port1.postMessage(arrayBuffer, [ arrayBuffer ]);
}, "Calling postMessage with an empty ArrayBuffer is valid.");
</script>
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