Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -578,17 +578,35 @@ void CopyImpl(Local<Value> source_obj,
void SlowCopy(const FunctionCallbackInfo<Value>& args) {
Local<Value> source_obj = args[0];
Local<Value> target_obj = args[1];
Isolate* isolate = args.GetIsolate();

// Add validation before call CopyImpl blindly
if (!source_obj->IsArrayBufferView() || !target_obj->IsArrayBufferView()) {
isolate->ThrowException(v8::Exception::TypeError(
String::NewFromUtf8Literal(isolate, "Arguments must be ArrayBufferViews")));
return;
}
const uint32_t target_start = args[2].As<Uint32>()->Value();
const uint32_t source_start = args[3].As<Uint32>()->Value();
const uint32_t to_copy = args[4].As<Uint32>()->Value();
size_t source_len = source_obj.As<ArrayBufferView>()->ByteLength();
size_t target_len = target_obj.As<ArrayBufferView>()->ByteLength();

if (source_start > source_len || target_start > target_len ||
to_copy > source_len - source_start ||
to_copy > target_len - target_start) {
isolate->ThrowException(v8::Exception::RangeError(
String::NewFromUtf8Literal(isolate, "Buffer copy out of range")));
return;
}

CopyImpl(source_obj, target_obj, target_start, source_start, to_copy);

args.GetReturnValue().Set(to_copy);
}

// Assume caller has properly validated args.
uint32_t FastCopy(Local<Value> receiver,
int32_t FastCopy(Local<Value> receiver,
Local<Value> source_obj,
Local<Value> target_obj,
uint32_t target_start,
Expand All @@ -597,7 +615,21 @@ uint32_t FastCopy(Local<Value> receiver,
// NOLINTNEXTLINE(runtime/references)
FastApiCallbackOptions& options) {
HandleScope scope(options.isolate);
Isolate* isolate = options.isolate;

if (!source_obj->IsArrayBufferView() || !target_obj->IsArrayBufferView()) {
isolate->ThrowException(v8::Exception::TypeError(
String::NewFromUtf8Literal(isolate, "Arguments must be ArrayBufferViews")));
return 0;
}
// Validate First before call CopyImpl blindly
size_t src_len = source_obj.As<ArrayBufferView>()->ByteLength();
size_t dst_len = target_obj.As<ArrayBufferView>()->ByteLength();

if (source_start > src_len || target_start > dst_len ||
to_copy > src_len - source_start || to_copy > dst_len - target_start) {
return -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be actually handled on the JS side, right?

}
CopyImpl(source_obj, target_obj, target_start, source_start, to_copy);

return to_copy;
Expand Down