Skip to content
Closed
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
buffer: fix out of range for toString
Co-authored-by: Michaël Zasso <targos@protonmail.com>
PR-URL: #54553
Fixes: #52298
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
  • Loading branch information
jazelly and targos committed Oct 3, 2024
commit e53b79cdd0a30ae12022772f95100e115f6f71e3
4 changes: 2 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,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);
largeBuffer.toString('utf8', threshold + 0xF, threshold + 0xFF);