Skip to content

Commit 5e8d38a

Browse files
committed
buffer: fix out of range for toString
1 parent 43f699d commit 5e8d38a

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/buffer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -843,12 +843,12 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
843843
else if (start >= len)
844844
return '';
845845
else
846-
start |= 0;
846+
start = MathTrunc(start) || 0;
847847

848848
if (end === undefined || end > len)
849849
end = len;
850850
else
851-
end |= 0;
851+
end = MathTrunc(end) || 0;
852852

853853
if (end <= start)
854854
return '';

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

+5
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ assert.throws(() => {
9898
name: 'TypeError',
9999
message: 'Unknown encoding: null'
100100
});
101+
102+
103+
const largeBuffer = Buffer.alloc(2 ** 32);
104+
// Must not throw when start and end are within kMaxLength
105+
largeBuffer.toString('utf8', 2 ** 31 + 1, 2 ** 31 + 10);

0 commit comments

Comments
 (0)