Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

buffer: Avoid overrun with 'binary' encoding.

Fixes #1624.
  • Loading branch information...
koichik committed Sep 1, 2011
1 parent ef27d56 commit 96ede8cc9b946e23482f855b19428868cab6021a
Showing with 10 additions and 2 deletions.
  1. +4 −2 src/node_buffer.cc
  2. +6 −0 test/simple/test-buffer.js
@@ -667,9 +667,11 @@ Handle<Value> Buffer::BinaryWrite(const Arguments &args) {

char *p = (char*)buffer->data_ + offset;

size_t towrite = MIN((unsigned long) s->Length(), buffer->length_ - offset);
size_t max_length = args[2]->IsUndefined() ? buffer->length_ - offset
: args[2]->Uint32Value();
max_length = MIN(s->Length(), MIN(buffer->length_ - offset, max_length));

int written = DecodeWrite(p, towrite, s, BINARY);
int written = DecodeWrite(p, max_length, s, BINARY);
return scope.Close(Integer::New(written));
}

@@ -553,3 +553,9 @@ assert.equal(written, 9);
written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
assert.equal(written, 10);

// test for buffer overrun
buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
var sub = buf.slice(0, 4); // length: 4
written = sub.write('12345', 'binary');
assert.equal(written, 4);
assert.equal(buf[4], 0);

0 comments on commit 96ede8c

Please sign in to comment.
You can’t perform that action at this time.