Skip to content

Commit 5229ffa

Browse files
addaleaxruyadorno
authored andcommitted
buffer: adjust validation to account for buffer.kMaxLength
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>
1 parent f21a5c6 commit 5229ffa

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/buffer.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ const {
9595
} = require('internal/errors');
9696
const {
9797
validateBuffer,
98-
validateInt32,
98+
validateInteger,
9999
validateString
100100
} = require('internal/validators');
101+
// Provide validateInteger() but with kMaxLength as the default maximum value.
102+
const validateOffset = (value, name, min = 0, max = kMaxLength) =>
103+
validateInteger(value, name, min, max);
101104

102105
const {
103106
FastBuffer,
@@ -557,7 +560,7 @@ Buffer.concat = function concat(list, length) {
557560
}
558561
}
559562
} else {
560-
validateInt32(length, 'length', 0);
563+
validateOffset(length, 'length');
561564
}
562565

563566
const buffer = Buffer.allocUnsafe(length);
@@ -864,22 +867,22 @@ Buffer.prototype.compare = function compare(target,
864867
if (targetStart === undefined)
865868
targetStart = 0;
866869
else
867-
validateInt32(targetStart, 'targetStart', 0);
870+
validateOffset(targetStart, 'targetStart');
868871

869872
if (targetEnd === undefined)
870873
targetEnd = target.length;
871874
else
872-
validateInt32(targetEnd, 'targetEnd', 0, target.length);
875+
validateOffset(targetEnd, 'targetEnd', 0, target.length);
873876

874877
if (sourceStart === undefined)
875878
sourceStart = 0;
876879
else
877-
validateInt32(sourceStart, 'sourceStart', 0);
880+
validateOffset(sourceStart, 'sourceStart');
878881

879882
if (sourceEnd === undefined)
880883
sourceEnd = this.length;
881884
else
882-
validateInt32(sourceEnd, 'sourceEnd', 0, this.length);
885+
validateOffset(sourceEnd, 'sourceEnd', 0, this.length);
883886

884887
if (sourceStart >= sourceEnd)
885888
return (targetStart >= targetEnd ? 0 : -1);
@@ -1003,12 +1006,12 @@ function _fill(buf, value, offset, end, encoding) {
10031006
offset = 0;
10041007
end = buf.length;
10051008
} else {
1006-
validateInt32(offset, 'offset', 0);
1009+
validateOffset(offset, 'offset');
10071010
// Invalid ranges are not set to a default, so can range check early.
10081011
if (end === undefined) {
10091012
end = buf.length;
10101013
} else {
1011-
validateInt32(end, 'end', 0, buf.length);
1014+
validateOffset(end, 'end', 0, buf.length);
10121015
}
10131016
if (offset >= end)
10141017
return buf;
@@ -1048,7 +1051,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
10481051

10491052
// Buffer#write(string, offset[, length][, encoding])
10501053
} else {
1051-
validateInt32(offset, 'offset', 0, this.length);
1054+
validateOffset(offset, 'offset', 0, this.length);
10521055

10531056
const remaining = this.length - offset;
10541057

@@ -1058,7 +1061,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
10581061
encoding = length;
10591062
length = remaining;
10601063
} else {
1061-
validateInt32(length, 'length', 0, this.length);
1064+
validateOffset(length, 'length', 0, this.length);
10621065
if (length > remaining)
10631066
length = remaining;
10641067
}

0 commit comments

Comments
 (0)