From 02ed0ec93bc8f6bcd7f1d629dca64ba1f335ea05 Mon Sep 17 00:00:00 2001 From: Matt Ranney Date: Tue, 29 Jun 2010 20:10:01 -0700 Subject: [PATCH] On overlapping buffers use memmove --- src/node_buffer.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 5253b04f92..95b6bcf7cf 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -323,9 +323,16 @@ Handle Buffer::Copy(const Arguments &args) { ssize_t to_copy = MIN(source_end - source_start, target->length() - target_start); - memcpy((void*)(target->data() + target_start), - (const void*)(source->data() + source_start), - to_copy); + if (target->blob_ == source->blob_) { + // need to use slightly slower memmove is the ranges might overlap + memmove((void*)(target->data() + target_start), + (const void*)(source->data() + source_start), + to_copy); + } else { + memcpy((void*)(target->data() + target_start), + (const void*)(source->data() + source_start), + to_copy); + } return scope.Close(Integer::New(to_copy)); }