Skip to content

Commit

Permalink
buffer: adjust validation to account for buffer.kMaxLength
Browse files Browse the repository at this point in the history
PR-URL: #35134
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
addaleax authored and ruyadorno committed Sep 17, 2020
1 parent f21a5c6 commit 5229ffa
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ const {
} = require('internal/errors');
const {
validateBuffer,
validateInt32,
validateInteger,
validateString
} = require('internal/validators');
// Provide validateInteger() but with kMaxLength as the default maximum value.
const validateOffset = (value, name, min = 0, max = kMaxLength) =>
validateInteger(value, name, min, max);

const {
FastBuffer,
Expand Down Expand Up @@ -557,7 +560,7 @@ Buffer.concat = function concat(list, length) {
}
}
} else {
validateInt32(length, 'length', 0);
validateOffset(length, 'length');
}

const buffer = Buffer.allocUnsafe(length);
Expand Down Expand Up @@ -864,22 +867,22 @@ Buffer.prototype.compare = function compare(target,
if (targetStart === undefined)
targetStart = 0;
else
validateInt32(targetStart, 'targetStart', 0);
validateOffset(targetStart, 'targetStart');

if (targetEnd === undefined)
targetEnd = target.length;
else
validateInt32(targetEnd, 'targetEnd', 0, target.length);
validateOffset(targetEnd, 'targetEnd', 0, target.length);

if (sourceStart === undefined)
sourceStart = 0;
else
validateInt32(sourceStart, 'sourceStart', 0);
validateOffset(sourceStart, 'sourceStart');

if (sourceEnd === undefined)
sourceEnd = this.length;
else
validateInt32(sourceEnd, 'sourceEnd', 0, this.length);
validateOffset(sourceEnd, 'sourceEnd', 0, this.length);

if (sourceStart >= sourceEnd)
return (targetStart >= targetEnd ? 0 : -1);
Expand Down Expand Up @@ -1003,12 +1006,12 @@ function _fill(buf, value, offset, end, encoding) {
offset = 0;
end = buf.length;
} else {
validateInt32(offset, 'offset', 0);
validateOffset(offset, 'offset');
// Invalid ranges are not set to a default, so can range check early.
if (end === undefined) {
end = buf.length;
} else {
validateInt32(end, 'end', 0, buf.length);
validateOffset(end, 'end', 0, buf.length);
}
if (offset >= end)
return buf;
Expand Down Expand Up @@ -1048,7 +1051,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {

// Buffer#write(string, offset[, length][, encoding])
} else {
validateInt32(offset, 'offset', 0, this.length);
validateOffset(offset, 'offset', 0, this.length);

const remaining = this.length - offset;

Expand All @@ -1058,7 +1061,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
encoding = length;
length = remaining;
} else {
validateInt32(length, 'length', 0, this.length);
validateOffset(length, 'length', 0, this.length);
if (length > remaining)
length = remaining;
}
Expand Down

0 comments on commit 5229ffa

Please sign in to comment.