Skip to content

Commit e371545

Browse files
addaleaxevanlucas
authored andcommitted
buffer: allow .write() offset to be at buffer end
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>
1 parent c2f5471 commit e371545

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/node_buffer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
714714
size_t max_length;
715715

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

720720
CHECK_NOT_OOB(ParseArrayIndex(args[2], ts_obj_length - offset, &max_length));

test/parallel/test-buffer-alloc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ writeTest.write('e', 3, 'ascii');
358358
writeTest.write('j', 4, 'ascii');
359359
assert.equal(writeTest.toString(), 'nodejs');
360360

361+
// Offset points to the end of the buffer
362+
// (see https://github.com/nodejs/node/issues/8127).
363+
assert.doesNotThrow(() => {
364+
Buffer.alloc(1).write('', 1, 0);
365+
});
366+
361367
// ASCII slice test
362368
{
363369
const asciiString = 'hello world';

0 commit comments

Comments
 (0)