Skip to content

Commit 0df499a

Browse files
jazellytargos
authored and
Jason Zhang
committed
buffer: fix out of range for toString
Co-authored-by: Michaël Zasso <[email protected]> PR-URL: nodejs#54553 Fixes: nodejs#52298 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jake Yuesong Li <[email protected]>
1 parent 47be236 commit 0df499a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/buffer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -841,12 +841,12 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
841841
else if (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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

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

66
const rangeBuffer = 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+
const threshold = 0xFFFFFFFF;
107+
const largeBuffer = Buffer.alloc(threshold);
108+
largeBuffer.toString('utf8', threshold + 0xF, threshold + 0xFF);

0 commit comments

Comments
 (0)