Skip to content

Commit

Permalink
buffer: allow .write() offset to be at buffer end
Browse files Browse the repository at this point in the history
Do not throw if the offset passed to `buf.write()` points
to the end of the buffer.

Fixes: #8127
PR-URL: #8154
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
  • Loading branch information
addaleax authored and jasnell committed Aug 22, 2016
1 parent d6d3229 commit 3242b27
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
size_t max_length;

CHECK_NOT_OOB(ParseArrayIndex(args[1], 0, &offset));
if (offset >= ts_obj_length)
if (offset > ts_obj_length)
return env->ThrowRangeError("Offset is out of bounds");

CHECK_NOT_OOB(ParseArrayIndex(args[2], ts_obj_length - offset, &max_length));
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ writeTest.write('e', 3, 'ascii');
writeTest.write('j', 4, 'ascii');
assert.equal(writeTest.toString(), 'nodejs');

// Offset points to the end of the buffer
// (see https://github.com/nodejs/node/issues/8127).
assert.doesNotThrow(() => {
Buffer.alloc(1).write('', 1, 0);
});

// ASCII slice test
{
const asciiString = 'hello world';
Expand Down

0 comments on commit 3242b27

Please sign in to comment.