Skip to content

Commit d964ffe

Browse files
seppevsBridgeAR
authored andcommitted
buffer: check byteLength in readInt(B|L)E
The 'byteLength' argument should be required and of type 'number'. It should have a value between 1 and 6. PR-URL: nodejs#11146 Fixes: nodejs#10515 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent a9e422e commit d964ffe

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

benchmark/buffers/buffer-read.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const types = [
1515
'FloatLE',
1616
'FloatBE',
1717
'DoubleLE',
18-
'DoubleBE'
18+
'DoubleBE',
19+
'IntLE',
20+
'IntBE',
1921
];
2022

2123
const bench = common.createBenchmark(main, {
@@ -34,11 +36,19 @@ function main(conf) {
3436
const fn = `read${type}`;
3537

3638
buff.writeDoubleLE(0, 0, noAssert);
37-
const testFunction = new Function('buff', `
38-
for (var i = 0; i !== ${len}; i++) {
39-
buff.${fn}(0, ${JSON.stringify(noAssert)});
40-
}
41-
`);
39+
40+
var call;
41+
if (fn === 'readIntLE' || fn === 'readIntBE') {
42+
call = `buff.${fn}(0, 1, ${JSON.stringify(noAssert)})`;
43+
} else {
44+
call = `buff.${fn}(0, ${JSON.stringify(noAssert)})`;
45+
}
46+
47+
const testFunction = new Function(
48+
'buff',
49+
`for (var i = 0; i !== ${len}; ++i) { ${call}; }`
50+
);
51+
4252
bench.start();
4353
testFunction(buff);
4454
bench.end(len / 1e6);

doc/api/buffer.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,8 +1776,11 @@ console.log(buf.readIntLE(0, 6).toString(16));
17761776
// Prints: 1234567890ab
17771777
console.log(buf.readIntBE(0, 6).toString(16));
17781778

1779-
// Throws an exception: RangeError: Index out of range
1779+
// Throws ERR_INDEX_OUT_OF_RANGE:
17801780
console.log(buf.readIntBE(1, 6).toString(16));
1781+
1782+
// Throws ERR_OUT_OF_RANGE:
1783+
console.log(buf.readIntBE(1, 0).toString(16));
17811784
```
17821785

17831786
### buf.readUInt8(offset[, noAssert])

lib/buffer.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,11 @@ Buffer.from = function from(value, encodingOrOffset, length) {
200200
);
201201
}
202202

203-
if (typeof value === 'number')
203+
if (typeof value === 'number') {
204204
throw new errors.TypeError(
205205
'ERR_INVALID_ARG_TYPE', 'value', 'not number', value
206206
);
207+
}
207208

208209
const valueOf = value.valueOf && value.valueOf();
209210
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
@@ -447,10 +448,11 @@ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
447448

448449
Buffer.concat = function concat(list, length) {
449450
var i;
450-
if (!Array.isArray(list))
451+
if (!Array.isArray(list)) {
451452
throw new errors.TypeError(
452453
'ERR_INVALID_ARG_TYPE', 'list', ['Array', 'Buffer', 'Uint8Array']
453454
);
455+
}
454456

455457
if (list.length === 0)
456458
return new FastBuffer();
@@ -467,10 +469,11 @@ Buffer.concat = function concat(list, length) {
467469
var pos = 0;
468470
for (i = 0; i < list.length; i++) {
469471
var buf = list[i];
470-
if (!isUint8Array(buf))
472+
if (!isUint8Array(buf)) {
471473
throw new errors.TypeError(
472474
'ERR_INVALID_ARG_TYPE', 'list', ['Array', 'Buffer', 'Uint8Array']
473475
);
476+
}
474477
_copy(buf, buffer, pos);
475478
pos += buf.length;
476479
}
@@ -1024,6 +1027,14 @@ function checkOffset(offset, ext, length) {
10241027
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
10251028
}
10261029

1030+
function checkByteLength(byteLength) {
1031+
if (byteLength < 1 || byteLength > 6) {
1032+
throw new errors.RangeError('ERR_OUT_OF_RANGE',
1033+
'byteLength',
1034+
'>= 1 and <= 6');
1035+
}
1036+
}
1037+
10271038

10281039
Buffer.prototype.readUIntLE =
10291040
function readUIntLE(offset, byteLength, noAssert) {
@@ -1109,8 +1120,11 @@ Buffer.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) {
11091120
Buffer.prototype.readIntLE = function readIntLE(offset, byteLength, noAssert) {
11101121
offset = offset >>> 0;
11111122
byteLength = byteLength >>> 0;
1112-
if (!noAssert)
1123+
1124+
if (!noAssert) {
1125+
checkByteLength(byteLength);
11131126
checkOffset(offset, byteLength, this.length);
1127+
}
11141128

11151129
var val = this[offset];
11161130
var mul = 1;
@@ -1129,8 +1143,11 @@ Buffer.prototype.readIntLE = function readIntLE(offset, byteLength, noAssert) {
11291143
Buffer.prototype.readIntBE = function readIntBE(offset, byteLength, noAssert) {
11301144
offset = offset >>> 0;
11311145
byteLength = byteLength >>> 0;
1132-
if (!noAssert)
1146+
1147+
if (!noAssert) {
1148+
checkByteLength(byteLength);
11331149
checkOffset(offset, byteLength, this.length);
1150+
}
11341151

11351152
var i = byteLength;
11361153
var mul = 1;
@@ -1612,9 +1629,10 @@ if (process.binding('config').hasIntl) {
16121629
// Transcodes the Buffer from one encoding to another, returning a new
16131630
// Buffer instance.
16141631
transcode = function transcode(source, fromEncoding, toEncoding) {
1615-
if (!isUint8Array(source))
1632+
if (!isUint8Array(source)) {
16161633
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'source',
16171634
['Buffer', 'Uint8Array'], source);
1635+
}
16181636
if (source.length === 0) return Buffer.alloc(0);
16191637

16201638
fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding;

test/parallel/test-buffer-read.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function read(buff, funx, args, expected) {
99

1010
assert.strictEqual(buff[funx](...args), expected);
1111
common.expectsError(
12-
() => buff[funx](-1),
12+
() => buff[funx](-1, args[1]),
1313
{
1414
code: 'ERR_INDEX_OUT_OF_RANGE'
1515
}
@@ -142,3 +142,19 @@ assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
142142
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
143143
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
144144
}
145+
146+
// test for byteLength parameter not between 1 and 6 (inclusive)
147+
common.expectsError(() => { buf.readIntLE(1); }, { code: 'ERR_OUT_OF_RANGE' });
148+
common.expectsError(() => { buf.readIntLE(1, 'string'); },
149+
{ code: 'ERR_OUT_OF_RANGE' });
150+
common.expectsError(() => { buf.readIntLE(1, 0); },
151+
{ code: 'ERR_OUT_OF_RANGE' });
152+
common.expectsError(() => { buf.readIntLE(1, 7); },
153+
{ code: 'ERR_OUT_OF_RANGE' });
154+
common.expectsError(() => { buf.readIntBE(1); }, { code: 'ERR_OUT_OF_RANGE' });
155+
common.expectsError(() => { buf.readIntBE(1, 'string'); },
156+
{ code: 'ERR_OUT_OF_RANGE' });
157+
common.expectsError(() => { buf.readIntBE(1, 0); },
158+
{ code: 'ERR_OUT_OF_RANGE' });
159+
common.expectsError(() => { buf.readIntBE(1, 7); },
160+
{ code: 'ERR_OUT_OF_RANGE' });

0 commit comments

Comments
 (0)