Skip to content

Commit 02ed0ec

Browse files
mranneyry
authored andcommitted
On overlapping buffers use memmove
1 parent 898afba commit 02ed0ec

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

‎src/node_buffer.cc‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,16 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
323323
ssize_t to_copy = MIN(source_end - source_start,
324324
target->length() - target_start);
325325

326-
memcpy((void*)(target->data() + target_start),
327-
(const void*)(source->data() + source_start),
328-
to_copy);
326+
if (target->blob_ == source->blob_) {
327+
// need to use slightly slower memmove is the ranges might overlap
328+
memmove((void*)(target->data() + target_start),
329+
(const void*)(source->data() + source_start),
330+
to_copy);
331+
} else {
332+
memcpy((void*)(target->data() + target_start),
333+
(const void*)(source->data() + source_start),
334+
to_copy);
335+
}
329336

330337
return scope.Close(Integer::New(to_copy));
331338
}

0 commit comments

Comments
 (0)