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
4 changes: 2 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,12 +843,12 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
else if (start >= len)
return '';
else
start |= 0;
start = MathTrunc(start) || 0;

if (end === undefined || end > len)
end = len;
else
end |= 0;
end = MathTrunc(end) || 0;

if (end <= start)
return '';
Expand Down
10 changes: 9 additions & 1 deletion test/parallel/test-buffer-tostring-range.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');

const rangeBuffer = Buffer.from('abc');
Expand Down Expand Up @@ -98,3 +98,11 @@ assert.throws(() => {
name: 'TypeError',
message: 'Unknown encoding: null'
});

// Must not throw when start and end are within kMaxLength
// Cannot test on 32bit machine as we are testing the case
// when start and end are above the threshold
common.skipIf32Bits();
const threshold = 0xFFFFFFFF;
const largeBuffer = Buffer.alloc(threshold + 20);
largeBuffer.toString('utf8', threshold, threshold + 20);