Skip to content

Commit

Permalink
fix(ext/node): Fix invalid length variable reference in blitBuffer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aapoalas authored Sep 24, 2023
1 parent 0e2637f commit cb9ab9c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ext/node/polyfills/internal/buffer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1760,22 +1760,23 @@ function utf8ToBytes(string, units) {
}

function blitBuffer(src, dst, offset, byteLength = Infinity) {
const srcLength = src.length;
// Establish the number of bytes to be written
const bytesToWrite = Math.min(
// If byte length is defined in the call, then it sets an upper bound,
// otherwise it is Infinity and is never chosen.
byteLength,
// The length of the source sets an upper bound being the source of data.
src.length,
srcLength,
// The length of the destination minus any offset into it sets an upper bound.
dst.length - offset,
);
if (bytesToWrite < src.length) {
if (bytesToWrite < srcLength) {
// Resize the source buffer to the number of bytes we're about to write.
// This both makes sure that we're actually only writing what we're told to
// write but also prevents `Uint8Array#set` from throwing an error if the
// source is longer than the target.
src = src.subarray(0, length);
src = src.subarray(0, bytesToWrite);
}
dst.set(src, offset);
return bytesToWrite;
Expand Down

0 comments on commit cb9ab9c

Please sign in to comment.