diff --git a/lib/buffer.js b/lib/buffer.js index f4dfba860f..942fc35071 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -789,11 +789,10 @@ Buffer.prototype.toJSON = function() { function adjustOffset(offset, length) { - offset = +offset; - if (offset === 0 || Number.isNaN(offset)) { + offset |= 0; + if (offset === 0) { return 0; - } - if (offset < 0) { + } else if (offset < 0) { offset += length; return offset > 0 ? offset : 0; } else { diff --git a/test/parallel/test-buffer-slice.js b/test/parallel/test-buffer-slice.js index 133d056b17..e5b598e625 100644 --- a/test/parallel/test-buffer-slice.js +++ b/test/parallel/test-buffer-slice.js @@ -62,3 +62,13 @@ assert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0); // slice(0,0).length === 0 assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length); + +{ + // Regression tests for https://github.com/nodejs/node/issues/9096 + const buf = Buffer.from('abcd'); + assert.strictEqual(buf.slice(buf.length / 3).toString(), 'bcd'); + assert.strictEqual( + buf.slice(buf.length / 3, buf.length).toString(), + 'bcd' + ); +}