Browse Source

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: https://github.com/nodejs/node/issues/8127
PR-URL: https://github.com/nodejs/node/pull/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>
v7.x
Anna Henningsen 9 years ago
committed by James M Snell
parent
commit
3242b27b54
  1. 2
      src/node_buffer.cc
  2. 6
      test/parallel/test-buffer-alloc.js

2
src/node_buffer.cc

@ -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));

6
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';

Loading…
Cancel
Save