Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,10 @@ Buffer.prototype.toJSON = function() {


function adjustOffset(offset, length) {
offset |= 0;
if (offset === 0) {
// Use Math.trunc() to convert offset to an integer value that can be larger
// than an Int32. Hence, don't use offset | 0 or similar techniques.
offset = Math.trunc(offset);
if (offset === 0 || Number.isNaN(offset)) {
return 0;
} else if (offset < 0) {
offset += length;
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-buffer-slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,30 @@ assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length);
'bcd'
);
}

{
const buf = Buffer.from('abcdefg');
assert.strictEqual(buf.slice(-(-1 >>> 0) - 1).toString(), buf.toString());
}

{
const buf = Buffer.from('abc');
assert.strictEqual(buf.slice(-0.5).toString(), buf.toString());
}

{
const buf = Buffer.from([
1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0,
0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0
]);
const chunk1 = Buffer.from([
1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0
]);
const chunk2 = Buffer.from([
0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0
]);
const middle = buf.length / 2;

assert.deepStrictEqual(buf.slice(0, middle), chunk1);
assert.deepStrictEqual(buf.slice(middle), chunk2);
}