buffer: adjust validation to account for buffer.kMaxLength

PR-URL: https://github.com/nodejs/node/pull/35134
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
This commit is contained in:
Anna Henningsen 2020-09-09 23:11:21 +02:00 committed by Node.js GitHub Bot
parent 541d296d56
commit 3c564632fa

View File

@ -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,
@ -541,7 +544,7 @@ Buffer.concat = function concat(list, length) {
}
}
} else {
validateInt32(length, 'length', 0);
validateOffset(length, 'length');
}
const buffer = Buffer.allocUnsafe(length);
@ -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);
@ -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;
@ -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;
@ -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;
}