Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6b3c24d

Browse filesBrowse files
jazellytargos
authored andcommitted
buffer: fix out of range for toString
Co-authored-by: Michaël Zasso <targos@protonmail.com> PR-URL: #54553 Backport-PR-URL: #55213 Fixes: #52298 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com> Refs: #54553
1 parent ae34e27 commit 6b3c24d
Copy full SHA for 6b3c24d

File tree

Expand file treeCollapse file tree

2 files changed

+11
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+11
-3
lines changed
Open diff view settings
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+2-2Lines 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
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 '';
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-buffer-tostring-range.js
+9-1Lines 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+
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.