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: nodejs#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 joesepi committed Oct 22, 2020
1 parent bc50a12 commit 3969298
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 @@ -93,9 +93,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 @@ -541,7 +544,7 @@ Buffer.concat = function concat(list, length) {
}
}
} else {
validateInt32(length, 'length', 0);
validateOffset(length, 'length');
}

const buffer = Buffer.allocUnsafe(length);
Expand Down Expand Up @@ -848,22 +851,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 @@ -987,12 +990,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 @@ -1032,7 +1035,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 @@ -1042,7 +1045,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 3969298

Please sign in to comment.