Skip to content

Commit 5404cbc

Browse files
committed
buffer: fix copy() segfault with zero arguments
Buffer#copy() immediately does a ToObject() on the first argument before it checks if it's even an Object. This causes Object::HasIndexedPropertiesInExternalArrayData() to be run on nothing, triggering the segfault. Instead run HasInstance() on the args Value. Which will check if it's actually an Object, before checking if it contains data. Fixes: #1519 PR-URL: #1520 Reviewed-by: Evan Lucas <[email protected]>
1 parent 2f6986e commit 5404cbc

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/node_buffer.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ void Base64Slice(const FunctionCallbackInfo<Value>& args) {
303303
void Copy(const FunctionCallbackInfo<Value> &args) {
304304
Environment* env = Environment::GetCurrent(args);
305305

306-
Local<Object> target = args[0]->ToObject(env->isolate());
307-
308-
if (!HasInstance(target))
306+
if (!HasInstance(args[0]))
309307
return env->ThrowTypeError("first arg should be a Buffer");
310308

309+
Local<Object> target = args[0]->ToObject(env->isolate());
310+
311311
ARGS_THIS(args.This())
312312
size_t target_length = target->GetIndexedPropertiesExternalArrayDataLength();
313313
char* target_data = static_cast<char*>(

test/parallel/test-buffer.js

+5
Original file line numberDiff line numberDiff line change
@@ -1179,3 +1179,8 @@ var ps = Buffer.poolSize;
11791179
Buffer.poolSize = 0;
11801180
assert.equal(Buffer(1).parent, undefined);
11811181
Buffer.poolSize = ps;
1182+
1183+
// Test Buffer.copy() segfault
1184+
assert.throws(function() {
1185+
Buffer(10).copy();
1186+
});

0 commit comments

Comments
 (0)