From e7c4f8cdaa5905a45fa990354023330f3309e4ae Mon Sep 17 00:00:00 2001 From: Peter Griess Date: Thu, 15 Jul 2010 08:52:15 -0500 Subject: [PATCH] Buffer.copy() should liberally allow empty copies. --- src/node_buffer.cc | 5 +++++ test/simple/test-buffer.js | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 8d35b7e6df..ce959d8498 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -305,6 +305,11 @@ Handle Buffer::Copy(const Arguments &args) { "sourceEnd < sourceStart"))); } + // Copy 0 bytes; we're done + if (source_end == source_start) { + return scope.Close(Integer::New(0)); + } + if (target_start < 0 || target_start >= target->length()) { return ThrowException(Exception::Error(String::New( "targetStart out of bounds"))); diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index 566cc4695c..a1495b0313 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -94,7 +94,7 @@ assert.strictEqual('sourceStart out of bounds', caught_error.message); // try to copy starting after the end of b caught_error = null; try { - var copied = b.copy(c, 0, 1024, 1024); + var copied = b.copy(c, 0, 1024, 1025); } catch (err) { caught_error = err; } @@ -110,6 +110,25 @@ try { } assert.strictEqual('sourceEnd out of bounds', caught_error.message); +// try to create 0-length buffers +new Buffer(''); +new Buffer('', 'ascii'); +new Buffer('', 'binary'); +new Buffer(0); + +// try to write a 0-length string beyond the end of b +b.write('', 1024); +b.write('', 2048); + +// try to copy 0 bytes worth of data into an empty buffer +b.copy(new Buffer(0), 0, 0, 0); + +// try to copy 0 bytes past the end of the target buffer +b.copy(new Buffer(0), 1, 1, 1); +b.copy(new Buffer(1), 1, 1, 1); + +// try to copy 0 bytes from past the end of the source buffer +b.copy(new Buffer(1), 0, 2048, 2048); var asciiString = "hello world"; var offset = 100;