mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
errors: use ERR_OUT_OF_RANGE for index errors
Remove ERR_INDEX_OUT_OF_RANGE in favor of ERR_OUT_OF_RANGE which is capable of providing more detail. (In one instance, use ERR_BUFFER_OUT_OF_BOUNDS which is more accurate in that one instance.) PR-URL: https://github.com/nodejs/node/pull/22969 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com>
This commit is contained in:
1 parent
884dbe9b7f
commit
f8d69911be
9 files changed
+104
-85
No files matched your search
+56
-55
@@ -61,7 +61,7 @@ const {
|
||||
} = process.binding('config');
|
||||
const {
|
||||
ERR_BUFFER_OUT_OF_BOUNDS,
|
||||
ERR_INDEX_OUT_OF_RANGE,
|
||||
ERR_OUT_OF_RANGE,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
ERR_INVALID_ARG_VALUE,
|
||||
ERR_INVALID_BUFFER_SIZE,
|
||||
@@ -692,50 +692,51 @@ Buffer.prototype[customInspectSymbol] = function inspect() {
|
||||
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];
|
||||
|
||||
Buffer.prototype.compare = function compare(target,
|
||||
start,
|
||||
end,
|
||||
thisStart,
|
||||
thisEnd) {
|
||||
targetStart,
|
||||
targetEnd,
|
||||
sourceStart,
|
||||
sourceEnd) {
|
||||
if (!isUint8Array(target)) {
|
||||
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
|
||||
}
|
||||
if (arguments.length === 1)
|
||||
return _compare(this, target);
|
||||
|
||||
if (start === undefined)
|
||||
start = 0;
|
||||
else if (start < 0)
|
||||
throw new ERR_INDEX_OUT_OF_RANGE();
|
||||
if (targetStart === undefined)
|
||||
targetStart = 0;
|
||||
else if (targetStart < 0)
|
||||
throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart);
|
||||
else
|
||||
start >>>= 0;
|
||||
targetStart >>>= 0;
|
||||
|
||||
if (end === undefined)
|
||||
end = target.length;
|
||||
else if (end > target.length)
|
||||
throw new ERR_INDEX_OUT_OF_RANGE();
|
||||
if (targetEnd === undefined)
|
||||
targetEnd = target.length;
|
||||
else if (targetEnd > target.length)
|
||||
throw new ERR_OUT_OF_RANGE('targetEnd', `<= ${target.length}`, targetEnd);
|
||||
else
|
||||
end >>>= 0;
|
||||
targetEnd >>>= 0;
|
||||
|
||||
if (thisStart === undefined)
|
||||
thisStart = 0;
|
||||
else if (thisStart < 0)
|
||||
throw new ERR_INDEX_OUT_OF_RANGE();
|
||||
if (sourceStart === undefined)
|
||||
sourceStart = 0;
|
||||
else if (sourceStart < 0)
|
||||
throw new ERR_OUT_OF_RANGE('sourceStart', '>= 0', sourceStart);
|
||||
else
|
||||
thisStart >>>= 0;
|
||||
sourceStart >>>= 0;
|
||||
|
||||
if (thisEnd === undefined)
|
||||
thisEnd = this.length;
|
||||
else if (thisEnd > this.length)
|
||||
throw new ERR_INDEX_OUT_OF_RANGE();
|
||||
if (sourceEnd === undefined)
|
||||
sourceEnd = this.length;
|
||||
else if (sourceEnd > this.length)
|
||||
throw new ERR_OUT_OF_RANGE('sourceEnd', `<= ${this.length}`, sourceEnd);
|
||||
else
|
||||
thisEnd >>>= 0;
|
||||
sourceEnd >>>= 0;
|
||||
|
||||
if (thisStart >= thisEnd)
|
||||
return (start >= end ? 0 : -1);
|
||||
else if (start >= end)
|
||||
if (sourceStart >= sourceEnd)
|
||||
return (targetStart >= targetEnd ? 0 : -1);
|
||||
else if (targetStart >= targetEnd)
|
||||
return 1;
|
||||
|
||||
return compareOffset(this, target, start, thisStart, end, thisEnd);
|
||||
return compareOffset(this, target, targetStart, sourceStart, targetEnd,
|
||||
sourceEnd);
|
||||
};
|
||||
|
||||
// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
|
||||
@@ -827,15 +828,15 @@ Buffer.prototype.includes = function includes(val, byteOffset, encoding) {
|
||||
// buffer.fill(number[, offset[, end]])
|
||||
// buffer.fill(buffer[, offset[, end]])
|
||||
// buffer.fill(string[, offset[, end]][, encoding])
|
||||
Buffer.prototype.fill = function fill(val, start, end, encoding) {
|
||||
return _fill(this, val, start, end, encoding);
|
||||
Buffer.prototype.fill = function fill(value, offset, end, encoding) {
|
||||
return _fill(this, value, offset, end, encoding);
|
||||
};
|
||||
|
||||
function _fill(buf, val, start, end, encoding) {
|
||||
if (typeof val === 'string') {
|
||||
if (start === undefined || typeof start === 'string') {
|
||||
encoding = start;
|
||||
start = 0;
|
||||
function _fill(buf, value, offset, end, encoding) {
|
||||
if (typeof value === 'string') {
|
||||
if (offset === undefined || typeof offset === 'string') {
|
||||
encoding = offset;
|
||||
offset = 0;
|
||||
end = buf.length;
|
||||
} else if (typeof end === 'string') {
|
||||
encoding = end;
|
||||
@@ -848,48 +849,48 @@ function _fill(buf, val, start, end, encoding) {
|
||||
throw new ERR_UNKNOWN_ENCODING(encoding);
|
||||
}
|
||||
|
||||
if (val.length === 0) {
|
||||
// If val === '' default to zero.
|
||||
val = 0;
|
||||
} else if (val.length === 1) {
|
||||
// Fast path: If `val` fits into a single byte, use that numeric value.
|
||||
if (value.length === 0) {
|
||||
// If value === '' default to zero.
|
||||
value = 0;
|
||||
} else if (value.length === 1) {
|
||||
// Fast path: If `value` fits into a single byte, use that numeric value.
|
||||
if (normalizedEncoding === 'utf8') {
|
||||
const code = val.charCodeAt(0);
|
||||
const code = value.charCodeAt(0);
|
||||
if (code < 128) {
|
||||
val = code;
|
||||
value = code;
|
||||
}
|
||||
} else if (normalizedEncoding === 'latin1') {
|
||||
val = val.charCodeAt(0);
|
||||
value = value.charCodeAt(0);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
encoding = undefined;
|
||||
}
|
||||
|
||||
if (start === undefined) {
|
||||
start = 0;
|
||||
if (offset === undefined) {
|
||||
offset = 0;
|
||||
end = buf.length;
|
||||
} else {
|
||||
// Invalid ranges are not set to a default, so can range check early.
|
||||
if (offset < 0)
|
||||
throw new ERR_OUT_OF_RANGE('offset', '>= 0', offset);
|
||||
if (end === undefined) {
|
||||
if (start < 0)
|
||||
throw new ERR_INDEX_OUT_OF_RANGE();
|
||||
end = buf.length;
|
||||
} else {
|
||||
if (start < 0 || end > buf.length || end < 0)
|
||||
throw new ERR_INDEX_OUT_OF_RANGE();
|
||||
if (end > buf.length || end < 0)
|
||||
throw new ERR_OUT_OF_RANGE('end', `>= 0 and <= ${buf.length}`, end);
|
||||
end = end >>> 0;
|
||||
}
|
||||
start = start >>> 0;
|
||||
if (start >= end)
|
||||
offset = offset >>> 0;
|
||||
if (offset >= end)
|
||||
return buf;
|
||||
}
|
||||
|
||||
const res = bindingFill(buf, val, start, end, encoding);
|
||||
const res = bindingFill(buf, value, offset, end, encoding);
|
||||
if (res < 0) {
|
||||
if (res === -1)
|
||||
throw new ERR_INVALID_ARG_VALUE('value', val);
|
||||
throw new ERR_INDEX_OUT_OF_RANGE();
|
||||
throw new ERR_INVALID_ARG_VALUE('value', value);
|
||||
throw new ERR_BUFFER_OUT_OF_BOUNDS();
|
||||
}
|
||||
|
||||
return buf;
|
||||
|
||||
Reference in new issue
Block a user