diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-15 19:51:06 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-29 16:52:46 +0200 |
commit | 60b5b38b4854005f11f008b510a57eab24025599 (patch) | |
tree | 6d146ce0f3b762b4a71652321b8b6ba706253dfd /lib | |
parent | 198eb9c5d6ac6a90dadb8c58396f9b35eaf6f5ce (diff) | |
download | android-node-v8-60b5b38b4854005f11f008b510a57eab24025599.tar.gz android-node-v8-60b5b38b4854005f11f008b510a57eab24025599.tar.bz2 android-node-v8-60b5b38b4854005f11f008b510a57eab24025599.zip |
buffer: do not always use defaults
The Buffer#(read|write)U?Int(B|L)E functions should not use a default
value. This is very likely a bug and it was never documented that
way.
Besides that this also improves the tests by adding more tests and by
refactoring them to less code lines.
PR-URL: https://github.com/nodejs/node/pull/20054
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/buffer.js | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index 085a82265a..54e13ff30b 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -59,6 +59,8 @@ function boundsError(value, length, type) { // Read integers. function readUIntLE(offset, byteLength) { + if (offset === undefined) + throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset); if (byteLength === 6) return readUInt48LE(this, offset); if (byteLength === 5) @@ -69,7 +71,7 @@ function readUIntLE(offset, byteLength) { return this.readUInt32LE(offset); if (byteLength === 2) return this.readUInt16LE(offset); - if (byteLength === 1 || byteLength === undefined) + if (byteLength === 1) return this.readUInt8(offset); boundsError(byteLength, 6, 'byteLength'); @@ -146,6 +148,8 @@ function readUInt8(offset = 0) { } function readUIntBE(offset, byteLength) { + if (offset === undefined) + throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset); if (byteLength === 6) return readUInt48BE(this, offset); if (byteLength === 5) @@ -156,7 +160,7 @@ function readUIntBE(offset, byteLength) { return this.readUInt32BE(offset); if (byteLength === 2) return this.readUInt16BE(offset); - if (byteLength === 1 || byteLength === undefined) + if (byteLength === 1) return this.readUInt8(offset); boundsError(byteLength, 6, 'byteLength'); @@ -224,6 +228,8 @@ function readUInt16BE(offset = 0) { } function readIntLE(offset, byteLength) { + if (offset === undefined) + throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset); if (byteLength === 6) return readInt48LE(this, offset); if (byteLength === 5) @@ -234,7 +240,7 @@ function readIntLE(offset, byteLength) { return this.readInt32LE(offset); if (byteLength === 2) return this.readInt16LE(offset); - if (byteLength === 1 || byteLength === undefined) + if (byteLength === 1) return this.readInt8(offset); boundsError(byteLength, 6, 'byteLength'); @@ -314,6 +320,8 @@ function readInt8(offset = 0) { } function readIntBE(offset, byteLength) { + if (offset === undefined) + throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset); if (byteLength === 6) return readInt48BE(this, offset); if (byteLength === 5) @@ -324,7 +332,7 @@ function readIntBE(offset, byteLength) { return this.readInt32BE(offset); if (byteLength === 2) return this.readInt16BE(offset); - if (byteLength === 1 || byteLength === undefined) + if (byteLength === 1) return this.readInt8(offset); boundsError(byteLength, 6, 'byteLength'); @@ -460,7 +468,7 @@ function readDoubleForwards(offset = 0) { } // Write integers. -function writeUIntLE(value, offset = 0, byteLength) { +function writeUIntLE(value, offset, byteLength) { if (byteLength === 6) return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff); if (byteLength === 5) @@ -471,7 +479,7 @@ function writeUIntLE(value, offset = 0, byteLength) { return writeU_Int32LE(this, value, offset, 0, 0xffffffff); if (byteLength === 2) return writeU_Int16LE(this, value, offset, 0, 0xffff); - if (byteLength === 1 || byteLength === undefined) + if (byteLength === 1) return writeU_Int8(this, value, offset, 0, 0xff); boundsError(byteLength, 6, 'byteLength'); @@ -571,7 +579,7 @@ function writeUInt8(value, offset = 0) { return writeU_Int8(this, value, offset, 0, 0xff); } -function writeUIntBE(value, offset = 0, byteLength) { +function writeUIntBE(value, offset, byteLength) { if (byteLength === 6) return writeU_Int48BE(this, value, offset, 0, 0xffffffffffffff); if (byteLength === 5) @@ -582,7 +590,7 @@ function writeUIntBE(value, offset = 0, byteLength) { return writeU_Int32BE(this, value, offset, 0, 0xffffffff); if (byteLength === 2) return writeU_Int16BE(this, value, offset, 0, 0xffff); - if (byteLength === 1 || byteLength === undefined) + if (byteLength === 1) return writeU_Int8(this, value, offset, 0, 0xff); boundsError(byteLength, 6, 'byteLength'); @@ -663,7 +671,7 @@ function writeUInt16BE(value, offset = 0) { return writeU_Int16BE(this, value, offset, 0, 0xffffffff); } -function writeIntLE(value, offset = 0, byteLength) { +function writeIntLE(value, offset, byteLength) { if (byteLength === 6) return writeU_Int48LE(this, value, offset, -0x800000000000, 0x7fffffffffff); if (byteLength === 5) @@ -674,7 +682,7 @@ function writeIntLE(value, offset = 0, byteLength) { return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff); if (byteLength === 2) return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff); - if (byteLength === 1 || byteLength === undefined) + if (byteLength === 1) return writeU_Int8(this, value, offset, -0x80, 0x7f); boundsError(byteLength, 6, 'byteLength'); @@ -692,7 +700,7 @@ function writeInt8(value, offset = 0) { return writeU_Int8(this, value, offset, -0x80, 0x7f); } -function writeIntBE(value, offset = 0, byteLength) { +function writeIntBE(value, offset, byteLength) { if (byteLength === 6) return writeU_Int48BE(this, value, offset, -0x800000000000, 0x7fffffffffff); if (byteLength === 5) @@ -703,7 +711,7 @@ function writeIntBE(value, offset = 0, byteLength) { return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff); if (byteLength === 2) return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff); - if (byteLength === 1 || byteLength === undefined) + if (byteLength === 1) return writeU_Int8(this, value, offset, -0x80, 0x7f); boundsError(byteLength, 6, 'byteLength'); |