diff --git a/src/node_buffer.cc b/src/node_buffer.cc index e685e4d119582d..cedcbafe972c56 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -714,7 +714,7 @@ void StringWrite(const FunctionCallbackInfo& 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)); diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index 76a34319c17278..24072a9a49c8bc 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -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';