Skip to content

Commit 6b3c24d

Browse files
jazellytargos
authored andcommitted
buffer: fix out of range for toString
Co-authored-by: Michaël Zasso <[email protected]> PR-URL: #54553 Backport-PR-URL: #55213Fixes: #52298 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jake Yuesong Li <[email protected]> Refs: #54553
1 parent ae34e27 commit 6b3c24d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

‎lib/buffer.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,12 +841,12 @@ Buffer.prototype.toString = function toString(encoding, start, end){
841841
elseif(start>=len)
842842
return'';
843843
else
844-
start|=0;
844+
start=MathTrunc(start)||0;
845845

846846
if(end===undefined||end>len)
847847
end=len;
848848
else
849-
end|=0;
849+
end=MathTrunc(end)||0;
850850

851851
if(end<=start)
852852
return'';

‎test/parallel/test-buffer-tostring-range.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
constcommon=require('../common');
44
constassert=require('assert');
55

66
constrangeBuffer=Buffer.from('abc');
@@ -98,3 +98,11 @@ assert.throws(() =>{
9898
name: 'TypeError',
9999
message: 'Unknown encoding: null'
100100
});
101+
102+
// Must not throw when start and end are within kMaxLength
103+
// Cannot test on 32bit machine as we are testing the case
104+
// when start and end are above the threshold
105+
common.skipIf32Bits();
106+
constthreshold=0xFFFFFFFF;
107+
constlargeBuffer=Buffer.alloc(threshold);
108+
largeBuffer.toString('utf8',threshold+0xF,threshold+0xFF);

0 commit comments

Comments
(0)