Skip to content

Commit 9fea7ea

Browse files
TrottBridgeAR
authored andcommitted
buffer: check byteLength in readUInt(B|L)E
PR-URL: #11146 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 94d6487 commit 9fea7ea

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

benchmark/buffers/buffer-read-with-byteLength.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
const common = require('../common.js');
33

44
const types = [
5-
'IntLE',
65
'IntBE',
6+
'IntLE',
7+
'UIntBE',
8+
'UIntLE'
79
];
810

911
const bench = common.createBenchmark(main, {

lib/buffer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,10 @@ Buffer.prototype.readUIntLE =
10401040
function readUIntLE(offset, byteLength, noAssert) {
10411041
offset = offset >>> 0;
10421042
byteLength = byteLength >>> 0;
1043-
if (!noAssert)
1043+
if (!noAssert) {
1044+
checkByteLength(byteLength);
10441045
checkOffset(offset, byteLength, this.length);
1046+
}
10451047

10461048
var val = this[offset];
10471049
var mul = 1;
@@ -1057,8 +1059,10 @@ Buffer.prototype.readUIntBE =
10571059
function readUIntBE(offset, byteLength, noAssert) {
10581060
offset = offset >>> 0;
10591061
byteLength = byteLength >>> 0;
1060-
if (!noAssert)
1062+
if (!noAssert) {
1063+
checkByteLength(byteLength);
10611064
checkOffset(offset, byteLength, this.length);
1065+
}
10621066

10631067
var val = this[offset + --byteLength];
10641068
var mul = 1;

test/parallel/test-buffer-read.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ read(buf, 'readUInt32BE', [1], 0xfd48eacf);
5757
read(buf, 'readUInt32LE', [1], 0xcfea48fd);
5858

5959
// testing basic functionality of readUIntBE() and readUIntLE()
60-
read(buf, 'readUIntBE', [2, 0], 0xfd);
61-
read(buf, 'readUIntLE', [2, 0], 0x48);
60+
read(buf, 'readUIntBE', [2, 2], 0x48ea);
61+
read(buf, 'readUIntLE', [2, 2], 0xea48);
62+
63+
// invalid byteLength parameter for readUIntBE() and readUIntLE()
64+
common.expectsError(() => { buf.readUIntBE(2, 0); },
65+
{ code: 'ERR_OUT_OF_RANGE' });
66+
common.expectsError(() => { buf.readUIntLE(2, 7); },
67+
{ code: 'ERR_OUT_OF_RANGE' });
6268

6369
// attempt to overflow buffers, similar to previous bug in array buffers
6470
assert.throws(() => Buffer.allocUnsafe(8).readFloatBE(0xffffffff),

0 commit comments

Comments
 (0)