buffer: fix out of range for toString

Co-authored-by: Michaël Zasso <targos@protonmail.com>
PR-URL: https://github.com/nodejs/node/pull/54553
Fixes: https://github.com/nodejs/node/issues/52298
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
This commit is contained in:
Jason Zhang 2024-09-07 03:09:24 +09:30 committed by GitHub
parent a75e3dff7e
commit 1c67899ffb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View File

@ -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 '';

View File

@ -1,6 +1,6 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const rangeBuffer = Buffer.from('abc');
@ -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);