Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: fix out of range for toString #54553

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
buffer: fix out of range for toString
Co-authored-by: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
jazelly and targos committed Aug 28, 2024
commit 0003f43ca37022fcd8b6f49445ce002e2be67052
4 changes: 2 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
@@ -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 '';
5 changes: 5 additions & 0 deletions test/parallel/test-buffer-tostring-range.js
Original file line number Diff line number Diff line change
@@ -98,3 +98,8 @@ assert.throws(() => {
name: 'TypeError',
message: 'Unknown encoding: null'
});


const largeBuffer = Buffer.alloc(2 ** 32);
// Must not throw when start and end are within kMaxLength
largeBuffer.toString('utf8', 2 ** 31 + 1, 2 ** 31 + 10);