summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/api.markdown4
-rw-r--r--lib/string_decoder.js4
-rw-r--r--src/node_buffer.cc21
-rw-r--r--test/simple/test-buffer.js12
4 files changed, 17 insertions, 24 deletions
diff --git a/doc/api.markdown b/doc/api.markdown
index 1ed9b808fa..e77756f429 100644
--- a/doc/api.markdown
+++ b/doc/api.markdown
@@ -161,7 +161,7 @@ buffer object. It does not change when the contents of the buffer are changed.
Does a memcpy() between buffers.
-Example: build two Buffers, then copy `buf1` from byte 16 through byte 20
+Example: build two Buffers, then copy `buf1` from byte 16 through byte 19
into `buf2`, starting at the 8th byte in `buf2`.
var Buffer = require('buffer').Buffer,
@@ -177,7 +177,7 @@ into `buf2`, starting at the 8th byte in `buf2`.
buf1.copy(buf2, 8, 16, 20);
console.log(buf2.toString('ascii', 0, 25));
- // !!!!!!!!qrstu!!!!!!!!!!!!
+ // !!!!!!!!qrst!!!!!!!!!!!!!
### buffer.slice(start, end)
diff --git a/lib/string_decoder.js b/lib/string_decoder.js
index 7bae2fc47b..2c7ace0ae5 100644
--- a/lib/string_decoder.js
+++ b/lib/string_decoder.js
@@ -25,7 +25,7 @@ StringDecoder.prototype.write = function (buffer) {
: buffer.length;
// add the new bytes to the char buffer
- buffer.copy(this.charBuffer, this.charReceived, 0, (i - 1));
+ buffer.copy(this.charBuffer, this.charReceived, 0, i);
this.charReceived += i;
if (this.charReceived < this.charLength) {
@@ -79,7 +79,7 @@ StringDecoder.prototype.write = function (buffer) {
}
// buffer the incomplete character bytes we got
- buffer.copy(this.charBuffer, 0, buffer.length - i, (buffer.length - 1));
+ buffer.copy(this.charBuffer, 0, buffer.length - i, buffer.length);
this.charReceived = i;
if (buffer.length - i > 0) {
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index 9a13b1ef64..5253b04f92 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -298,7 +298,7 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
ssize_t target_start = args[1]->Int32Value();
ssize_t source_start = args[2]->Int32Value();
ssize_t source_end = args[3]->IsInt32() ? args[3]->Int32Value()
- : (source->length() - 1);
+ : source->length();
if (source_end < source_start) {
return ThrowException(Exception::Error(String::New(
@@ -315,24 +315,17 @@ Handle<Value> Buffer::Copy(const Arguments &args) {
"sourceStart out of bounds")));
}
- if (source_end < 0 || source_end >= source->length()) {
+ if (source_end < 0 || source_end > source->length()) {
return ThrowException(Exception::Error(String::New(
"sourceEnd out of bounds")));
}
- ssize_t to_copy = MIN( (source_end - source_start + 1),
- (target->length() - target_start) );
+ ssize_t to_copy = MIN(source_end - source_start,
+ target->length() - target_start);
- if (source->handle_->StrictEquals(target->handle_)) {
- // 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);
- }
+ memcpy((void*)(target->data() + target_start),
+ (const void*)(source->data() + source_start),
+ to_copy);
return scope.Close(Integer::New(to_copy));
}
diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js
index 455efa6d3c..566cc4695c 100644
--- a/test/simple/test-buffer.js
+++ b/test/simple/test-buffer.js
@@ -19,8 +19,8 @@ for (var i = 0; i < 1024; i++) {
var c = new Buffer(512);
-// copy 512 bytes, from 0 to 511.
-var copied = b.copy(c, 0, 0, 511);
+// copy 512 bytes, from 0 to 512.
+var copied = b.copy(c, 0, 0, 512);
console.log("copied " + copied + " bytes from b into c");
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
@@ -30,7 +30,7 @@ for (var i = 0; i < c.length; i++) {
console.log("");
// try to copy 513 bytes, and hope we don't overrun c, which is only 512 long
-var copied = b.copy(c, 0, 0, 512);
+var copied = b.copy(c, 0, 0, 513);
console.log("copied " + copied + " bytes from b into c");
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
@@ -46,7 +46,7 @@ for (var i = 0; i < b.length; i++) {
}
// copy 768 bytes from b into b
-var copied = b.copy(b, 0, 256, 1023);
+var copied = b.copy(b, 0, 256, 1024);
console.log("copied " + copied + " bytes from b into c");
assert.strictEqual(768, copied);
for (var i = 0; i < c.length; i++) {
@@ -104,8 +104,8 @@ assert.strictEqual('sourceStart out of bounds', caught_error.message);
// try to copy ending after the end of b
try {
- var copied = b.copy(c, 0, 1023, 1024);
-} catch (err) {
+ var copied = b.copy(c, 0, 1023, 1025);
+} catch (err) {
caught_error = err;
}
assert.strictEqual('sourceEnd out of bounds', caught_error.message);