src: make a number of minor improvements to buffer · nodejs/node@7f1c95a
@@ -329,7 +329,8 @@ MaybeLocal<Object> New(Isolate* isolate,
329329if (actual > 0) [[likely]] {
330330if (actual < length) {
331331 std::unique_ptr<BackingStore> old_store = std::move(store);
332- store = ArrayBuffer::NewBackingStore(isolate, actual);
332+ store = ArrayBuffer::NewBackingStore(
333+ isolate, actual, BackingStoreInitializationMode::kUninitialized);
333334memcpy(store->Data(), old_store->Data(), actual);
334335 }
335336 Local<ArrayBuffer> buf = ArrayBuffer::New(isolate, std::move(store));
@@ -416,7 +417,7 @@ MaybeLocal<Object> Copy(Environment* env, const char* data, size_t length) {
416417417418CHECK(bs);
418419419-memcpy(bs->Data(), data, length);
420+if (length > 0) memcpy(bs->Data(), data, length);
420421421422 Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, std::move(bs));
422423@@ -506,6 +507,17 @@ MaybeLocal<Object> New(Environment* env,
506507 }
507508 }
508509510+#if defined(V8_ENABLE_SANDBOX)
511+// When v8 sandbox is enabled, external backing stores are not supported
512+// since all arraybuffer allocations are expected to be done by the isolate.
513+// Since this violates the contract of this function, let's free the data and
514+// throw an error.
515+free(data);
516+THROW_ERR_OPERATION_FAILED(
517+ env->isolate(),
518+"Wrapping external data is not supported when the v8 sandbox is enabled");
519+return MaybeLocal<Object>();
520+#else
509521 EscapableHandleScope handle_scope(env->isolate());
510522511523auto free_callback = [](void* data, size_t length, void* deleter_data) {
@@ -520,6 +532,7 @@ MaybeLocal<Object> New(Environment* env,
520532if (Buffer::New(env, ab, 0, length).ToLocal(&obj))
521533return handle_scope.Escape(obj);
522534return Local<Object>();
535+#endif
523536}
524537525538namespace {