Skip to content

Commit cbb2d60

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: nodejs#1519
1 parent 2f6986e commit cbb2d60

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/node_buffer.cc

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,3 +1179,6 @@ 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+
Buffer(10).copy();

0 commit comments

Comments
 (0)