Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
buffer: Avoid overrun with 'binary' encoding.
Browse files Browse the repository at this point in the history
Fixes #1624.
  • Loading branch information
koichik committed Sep 2, 2011
1 parent ef27d56 commit 96ede8c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
6 changes: 6 additions & 0 deletions test/simple/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.