From d36adb2209fa19a2cd612b3ea345ee38551b2585 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 18 Aug 2016 03:58:38 +0200 Subject: [PATCH 1/2] 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 --- src/node_buffer.cc | 2 +- test/parallel/test-buffer-alloc.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 388ee6b5692a93..ad06dd0233dc1c 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 df83fa83010832..47c65d0bca108a 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -358,6 +358,9 @@ writeTest.write('e', 3, 'ascii'); writeTest.write('j', 4, 'ascii'); assert.equal(writeTest.toString(), 'nodejs'); +// Does not throw (see https://github.com/nodejs/node/issues/8127). +Buffer.alloc(1).write('', 1, 0); + // ASCII slice test { const asciiString = 'hello world'; From 6f16518438c54b0c8eb1cde0948b638cacd84ceb Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 18 Aug 2016 16:49:05 +0200 Subject: [PATCH 2/2] use assert.doesNotThrow --- test/parallel/test-buffer-alloc.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index 47c65d0bca108a..00b63848d4e2d4 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -358,8 +358,11 @@ writeTest.write('e', 3, 'ascii'); writeTest.write('j', 4, 'ascii'); assert.equal(writeTest.toString(), 'nodejs'); -// Does not throw (see https://github.com/nodejs/node/issues/8127). -Buffer.alloc(1).write('', 1, 0); +// 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 {