Skip to content

Commit 234353a

Browse files
larissayvetteTrott
authored andcommitted
lib,src: refactor buffer out of range index
PR-URL: #11296 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 1474b7a commit 234353a

9 files changed

+72
-50
lines changed

doc/api/errors.md

+8
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,14 @@ found [here][online].
558558
the connected party did not properly respond after a period of time. Usually
559559
encountered by [`http`][] or [`net`][] -- often a sign that a `socket.end()`
560560
was not properly called.
561+
562+
<a id="ERROR_CODES"></a>
563+
### ERROR CODES
564+
565+
<a id="ERR_INDEX_OUT_OF_RANGE"></a>
566+
### ERR_INDEX_OUT_OF_RANGE
567+
568+
The `'ERR_INDEX_OUT_OF_RANGE'` error code is used when a given index is out of the accepted range.
561569

562570

563571
<a id="nodejs-error-codes"></a>

lib/buffer.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const { isAnyArrayBuffer, isUint8Array } = process.binding('util');
2828
const bindingObj = {};
2929
const internalUtil = require('internal/util');
3030
const pendingDeprecation = !!config.pendingDeprecation;
31+
const errors = require('internal/errors');
3132

3233
class FastBuffer extends Uint8Array {
3334
constructor(arg1, arg2, arg3) {
@@ -629,28 +630,28 @@ Buffer.prototype.compare = function compare(target,
629630
if (start === undefined)
630631
start = 0;
631632
else if (start < 0)
632-
throw new RangeError('out of range index');
633+
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
633634
else
634635
start >>>= 0;
635636

636637
if (end === undefined)
637638
end = target.length;
638639
else if (end > target.length)
639-
throw new RangeError('out of range index');
640+
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
640641
else
641642
end >>>= 0;
642643

643644
if (thisStart === undefined)
644645
thisStart = 0;
645646
else if (thisStart < 0)
646-
throw new RangeError('out of range index');
647+
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
647648
else
648649
thisStart >>>= 0;
649650

650651
if (thisEnd === undefined)
651652
thisEnd = this.length;
652653
else if (thisEnd > this.length)
653-
throw new RangeError('out of range index');
654+
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
654655
else
655656
thisEnd >>>= 0;
656657

@@ -795,7 +796,7 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
795796

796797
// Invalid ranges are not set to a default, so can range check early.
797798
if (start < 0 || end > this.length)
798-
throw new RangeError('Out of range index');
799+
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
799800

800801
if (end <= start)
801802
return this;
@@ -931,7 +932,7 @@ Buffer.prototype.slice = function slice(start, end) {
931932

932933
function checkOffset(offset, ext, length) {
933934
if (offset + ext > length)
934-
throw new RangeError('Index out of range');
935+
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
935936
}
936937

937938

@@ -1141,7 +1142,7 @@ function checkInt(buffer, value, offset, ext, max, min) {
11411142
if (value > max || value < min)
11421143
throw new TypeError('"value" argument is out of bounds');
11431144
if (offset + ext > buffer.length)
1144-
throw new RangeError('Index out of range');
1145+
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
11451146
}
11461147

11471148

lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
114114
E('ERR_ASSERTION', (msg) => msg);
115115
E('ERR_CONSOLE_WRITABLE_STREAM',
116116
(name) => `Console expects a writable stream instance for ${name}`);
117+
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
117118
E('ERR_INVALID_ARG_TYPE', invalidArgType);
118119
E('ERR_INVALID_CALLBACK', 'callback must be a function');
119120
E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s');

src/node_buffer.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
#define THROW_AND_RETURN_IF_OOB(r) \
4242
do { \
43-
if (!(r)) return env->ThrowRangeError("out of range index"); \
43+
if (!(r)) return env->ThrowRangeError("Index out of range"); \
4444
} while (0)
4545

4646
#define SLICE_START_END(start_arg, end_arg, end_max) \
@@ -564,7 +564,7 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
564564
return args.GetReturnValue().Set(0);
565565

566566
if (source_start > ts_obj_length)
567-
return env->ThrowRangeError("out of range index");
567+
return env->ThrowRangeError("Index out of range");
568568

569569
if (source_end - source_start > target_length - target_start)
570570
source_end = source_start + target_length - target_start;
@@ -878,9 +878,9 @@ void CompareOffset(const FunctionCallbackInfo<Value> &args) {
878878
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[5], ts_obj_length, &source_end));
879879

880880
if (source_start > ts_obj_length)
881-
return env->ThrowRangeError("out of range index");
881+
return env->ThrowRangeError("Index out of range");
882882
if (target_start > target_length)
883-
return env->ThrowRangeError("out of range index");
883+
return env->ThrowRangeError("Index out of range");
884884

885885
CHECK_LE(source_start, source_end);
886886
CHECK_LE(target_start, target_end);

test/parallel/test-buffer-alloc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,8 @@ assert.throws(() => {
963963
const a = Buffer.alloc(1);
964964
const b = Buffer.alloc(1);
965965
a.copy(b, 0, 0x100000000, 0x100000001);
966-
}, /out of range index/);
966+
}, common.expectsError(
967+
{code: undefined, type: RangeError, message: 'Index out of range'}));
967968

968969
// Unpooled buffer (replaces SlowBuffer)
969970
{

test/parallel/test-buffer-compare-offset.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55

66
const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
@@ -57,7 +57,7 @@ assert.strictEqual(1, a.compare(b, Infinity, -Infinity));
5757
// zero length target because default for targetEnd <= targetSource
5858
assert.strictEqual(1, a.compare(b, '0xff'));
5959

60-
const oor = /out of range index/;
60+
const oor = common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'});
6161

6262
assert.throws(() => a.compare(b, 0, 100, 0), oor);
6363
assert.throws(() => a.compare(b, 0, 1, 0, 100), oor);

test/parallel/test-buffer-fill.js

+44-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
// Flags: --expose-internals
12
'use strict';
2-
3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
5+
const errors = require('internal/errors');
56
const SIZE = 28;
67

78
const buf1 = Buffer.allocUnsafe(SIZE);
@@ -191,25 +192,30 @@ deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
191192

192193

193194
// Check exceptions
194-
assert.throws(() => buf1.fill(0, -1), /^RangeError: Out of range index$/);
195-
assert.throws(() =>
196-
buf1.fill(0, 0, buf1.length + 1),
197-
/^RangeError: Out of range index$/);
198-
assert.throws(() => buf1.fill('', -1), /^RangeError: Out of range index$/);
199-
assert.throws(() =>
200-
buf1.fill('', 0, buf1.length + 1),
201-
/^RangeError: Out of range index$/);
202-
assert.throws(() =>
203-
buf1.fill('a', 0, buf1.length, 'node rocks!'),
204-
/^TypeError: Unknown encoding: node rocks!$/);
205-
assert.throws(() =>
206-
buf1.fill('a', 0, 0, NaN),
207-
/^TypeError: encoding must be a string$/);
208-
assert.throws(() =>
209-
buf1.fill('a', 0, 0, null),
210-
/^TypeError: encoding must be a string$/);
211-
assert.throws(() =>
212-
buf1.fill('a', 0, 0, 'foo'), /^TypeError: Unknown encoding: foo$/);
195+
assert.throws(
196+
() => buf1.fill(0, -1),
197+
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
198+
assert.throws(
199+
() => buf1.fill(0, 0, buf1.length + 1),
200+
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
201+
assert.throws(
202+
() => buf1.fill('', -1),
203+
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
204+
assert.throws(
205+
() => buf1.fill('', 0, buf1.length + 1),
206+
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
207+
assert.throws(
208+
() => buf1.fill('a', 0, buf1.length, 'node rocks!'),
209+
/^TypeError: Unknown encoding: node rocks!$/);
210+
assert.throws(
211+
() => buf1.fill('a', 0, 0, NaN),
212+
/^TypeError: encoding must be a string$/);
213+
assert.throws(
214+
() => buf1.fill('a', 0, 0, null),
215+
/^TypeError: encoding must be a string$/);
216+
assert.throws(
217+
() => buf1.fill('a', 0, 0, 'foo'),
218+
/^TypeError: Unknown encoding: foo$/);
213219

214220

215221
function genBuffer(size, args) {
@@ -241,7 +247,7 @@ function writeToFill(string, offset, end, encoding) {
241247
}
242248

243249
if (offset < 0 || end > buf2.length)
244-
throw new RangeError('Out of range index');
250+
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
245251

246252
if (end <= offset)
247253
return buf2;
@@ -279,12 +285,12 @@ function testBufs(string, offset, length, encoding) {
279285
}
280286

281287
// Make sure these throw.
282-
assert.throws(() =>
283-
Buffer.allocUnsafe(8).fill('a', -1),
284-
/^RangeError: Out of range index$/);
285-
assert.throws(() =>
286-
Buffer.allocUnsafe(8).fill('a', 0, 9),
287-
/^RangeError: Out of range index$/);
288+
assert.throws(
289+
() => Buffer.allocUnsafe(8).fill('a', -1),
290+
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
291+
assert.throws(
292+
() => Buffer.allocUnsafe(8).fill('a', 0, 9),
293+
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
288294

289295
// Make sure this doesn't hang indefinitely.
290296
Buffer.allocUnsafe(8).fill('');
@@ -350,7 +356,8 @@ Buffer.alloc(8, '');
350356
}
351357
};
352358
Buffer.alloc(1).fill(Buffer.alloc(1), start, 1);
353-
}, /out of range index/);
359+
}, common.expectsError(
360+
{code: undefined, type: RangeError, message: 'Index out of range'}));
354361
// Make sure -1 is making it to Buffer::Fill().
355362
assert.ok(elseWasLast,
356363
'internal API changed, -1 no longer in correct location');
@@ -360,7 +367,8 @@ Buffer.alloc(8, '');
360367
// around.
361368
assert.throws(() => {
362369
process.binding('buffer').fill(Buffer.alloc(1), 1, -1, 0, 1);
363-
}, /out of range index/);
370+
}, common.expectsError(
371+
{code: undefined, type: RangeError, message: 'Index out of range'}));
364372

365373
// Make sure "end" is properly checked, even if it's magically mangled using
366374
// Symbol.toPrimitive.
@@ -383,7 +391,8 @@ assert.throws(() => {
383391
}
384392
};
385393
Buffer.alloc(1).fill(Buffer.alloc(1), 0, end);
386-
}, /^RangeError: out of range index$/);
394+
}, common.expectsError(
395+
{code: undefined, type: RangeError, message: 'Index out of range'}));
387396
// Make sure -1 is making it to Buffer::Fill().
388397
assert.ok(elseWasLast,
389398
'internal API changed, -1 no longer in correct location');
@@ -393,7 +402,8 @@ assert.throws(() => {
393402
// around.
394403
assert.throws(() => {
395404
process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1);
396-
}, /out of range index/);
405+
}, common.expectsError(
406+
{ code: undefined, type: RangeError, message: 'Index out of range'}));
397407

398408
// Test that bypassing 'length' won't cause an abort.
399409
assert.throws(() => {
@@ -403,7 +413,8 @@ assert.throws(() => {
403413
enumerable: true
404414
});
405415
buf.fill('');
406-
}, /^RangeError: out of range index$/);
416+
}, common.expectsError(
417+
{ code: undefined, type: RangeError, message: 'Index out of range'}));
407418

408419
assert.deepStrictEqual(
409420
Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'),

test/parallel/test-buffer-read.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44

55
// testing basic buffer read functions
@@ -10,7 +10,7 @@ function read(buff, funx, args, expected) {
1010
assert.strictEqual(buff[funx](...args), expected);
1111
assert.throws(
1212
() => buff[funx](-1),
13-
/^RangeError: Index out of range$/
13+
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})
1414
);
1515

1616
assert.doesNotThrow(

test/parallel/test-buffer-write-noassert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require('assert');
44

55
// testing buffer write functions
66

7-
const outOfRange = /^RangeError: (?:Index )?out of range(?: index)?$/;
7+
const outOfRange = /^RangeError\b.*\bIndex out of range$/;
88

99
function write(funx, args, result, res) {
1010
{

0 commit comments

Comments
 (0)