@@ -95,9 +95,12 @@ const { |
95 | 95 | } = require('internal/errors'); |
96 | 96 | const { |
97 | 97 | validateBuffer, |
98 | | - validateInt32, |
| 98 | + validateInteger, |
99 | 99 | validateString |
100 | 100 | } = 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); |
101 | 104 |
|
102 | 105 | const { |
103 | 106 | FastBuffer, |
@@ -557,7 +560,7 @@ Buffer.concat = function concat(list, length) { |
557 | 560 | } |
558 | 561 | } |
559 | 562 | } else { |
560 | | - validateInt32(length, 'length', 0); |
| 563 | + validateOffset(length, 'length'); |
561 | 564 | } |
562 | 565 |
|
563 | 566 | const buffer = Buffer.allocUnsafe(length); |
@@ -864,22 +867,22 @@ Buffer.prototype.compare = function compare(target, |
864 | 867 | if (targetStart === undefined) |
865 | 868 | targetStart = 0; |
866 | 869 | else |
867 | | - validateInt32(targetStart, 'targetStart', 0); |
| 870 | + validateOffset(targetStart, 'targetStart'); |
868 | 871 |
|
869 | 872 | if (targetEnd === undefined) |
870 | 873 | targetEnd = target.length; |
871 | 874 | else |
872 | | - validateInt32(targetEnd, 'targetEnd', 0, target.length); |
| 875 | + validateOffset(targetEnd, 'targetEnd', 0, target.length); |
873 | 876 |
|
874 | 877 | if (sourceStart === undefined) |
875 | 878 | sourceStart = 0; |
876 | 879 | else |
877 | | - validateInt32(sourceStart, 'sourceStart', 0); |
| 880 | + validateOffset(sourceStart, 'sourceStart'); |
878 | 881 |
|
879 | 882 | if (sourceEnd === undefined) |
880 | 883 | sourceEnd = this.length; |
881 | 884 | else |
882 | | - validateInt32(sourceEnd, 'sourceEnd', 0, this.length); |
| 885 | + validateOffset(sourceEnd, 'sourceEnd', 0, this.length); |
883 | 886 |
|
884 | 887 | if (sourceStart >= sourceEnd) |
885 | 888 | return (targetStart >= targetEnd ? 0 : -1); |
@@ -1003,12 +1006,12 @@ function _fill(buf, value, offset, end, encoding) { |
1003 | 1006 | offset = 0; |
1004 | 1007 | end = buf.length; |
1005 | 1008 | } else { |
1006 | | - validateInt32(offset, 'offset', 0); |
| 1009 | + validateOffset(offset, 'offset'); |
1007 | 1010 | // Invalid ranges are not set to a default, so can range check early. |
1008 | 1011 | if (end === undefined) { |
1009 | 1012 | end = buf.length; |
1010 | 1013 | } else { |
1011 | | - validateInt32(end, 'end', 0, buf.length); |
| 1014 | + validateOffset(end, 'end', 0, buf.length); |
1012 | 1015 | } |
1013 | 1016 | if (offset >= end) |
1014 | 1017 | return buf; |
@@ -1048,7 +1051,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) { |
1048 | 1051 |
|
1049 | 1052 | // Buffer#write(string, offset[, length][, encoding]) |
1050 | 1053 | } else { |
1051 | | - validateInt32(offset, 'offset', 0, this.length); |
| 1054 | + validateOffset(offset, 'offset', 0, this.length); |
1052 | 1055 |
|
1053 | 1056 | const remaining = this.length - offset; |
1054 | 1057 |
|
@@ -1058,7 +1061,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) { |
1058 | 1061 | encoding = length; |
1059 | 1062 | length = remaining; |
1060 | 1063 | } else { |
1061 | | - validateInt32(length, 'length', 0, this.length); |
| 1064 | + validateOffset(length, 'length', 0, this.length); |
1062 | 1065 | if (length > remaining) |
1063 | 1066 | length = remaining; |
1064 | 1067 | } |
|