diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index f72f5615fb26ce..3d9d4404be6560 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -57,8 +57,6 @@ function boundsError(value, length, type) { // Read integers. function readUIntLE(offset, byteLength) { - if (offset === undefined) - throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined); if (byteLength === 6) return readUInt48LE(this, offset); if (byteLength === 5) @@ -69,7 +67,7 @@ function readUIntLE(offset, byteLength) { return this.readUInt32LE(offset); if (byteLength === 2) return this.readUInt16LE(offset); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return this.readUInt8(offset); boundsError(byteLength, 6, 'byteLength'); @@ -146,8 +144,6 @@ function readUInt8(offset = 0) { } function readUIntBE(offset, byteLength) { - if (offset === undefined) - throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined); if (byteLength === 6) return readUInt48BE(this, offset); if (byteLength === 5) @@ -158,7 +154,7 @@ function readUIntBE(offset, byteLength) { return this.readUInt32BE(offset); if (byteLength === 2) return this.readUInt16BE(offset); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return this.readUInt8(offset); boundsError(byteLength, 6, 'byteLength'); @@ -226,8 +222,6 @@ function readUInt16BE(offset = 0) { } function readIntLE(offset, byteLength) { - if (offset === undefined) - throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined); if (byteLength === 6) return readInt48LE(this, offset); if (byteLength === 5) @@ -238,7 +232,7 @@ function readIntLE(offset, byteLength) { return this.readInt32LE(offset); if (byteLength === 2) return this.readInt16LE(offset); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return this.readInt8(offset); boundsError(byteLength, 6, 'byteLength'); @@ -318,8 +312,6 @@ function readInt8(offset = 0) { } function readIntBE(offset, byteLength) { - if (offset === undefined) - throw new ERR_INVALID_ARG_TYPE('offset', 'number', undefined); if (byteLength === 6) return readInt48BE(this, offset); if (byteLength === 5) @@ -330,7 +322,7 @@ function readIntBE(offset, byteLength) { return this.readInt32BE(offset); if (byteLength === 2) return this.readInt16BE(offset); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return this.readInt8(offset); boundsError(byteLength, 6, 'byteLength'); @@ -466,7 +458,7 @@ function readDoubleForwards(offset = 0) { } // Write integers. -function writeUIntLE(value, offset, byteLength) { +function writeUIntLE(value, offset = 0, byteLength) { if (byteLength === 6) return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff); if (byteLength === 5) @@ -477,7 +469,7 @@ function writeUIntLE(value, offset, byteLength) { return writeU_Int32LE(this, value, offset, 0, 0xffffffff); if (byteLength === 2) return writeU_Int16LE(this, value, offset, 0, 0xffff); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return writeU_Int8(this, value, offset, 0, 0xff); boundsError(byteLength, 6, 'byteLength'); @@ -577,7 +569,7 @@ function writeUInt8(value, offset = 0) { return writeU_Int8(this, value, offset, 0, 0xff); } -function writeUIntBE(value, offset, byteLength) { +function writeUIntBE(value, offset = 0, byteLength) { if (byteLength === 6) return writeU_Int48BE(this, value, offset, 0, 0xffffffffffffff); if (byteLength === 5) @@ -588,7 +580,7 @@ function writeUIntBE(value, offset, byteLength) { return writeU_Int32BE(this, value, offset, 0, 0xffffffff); if (byteLength === 2) return writeU_Int16BE(this, value, offset, 0, 0xffff); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return writeU_Int8(this, value, offset, 0, 0xff); boundsError(byteLength, 6, 'byteLength'); @@ -669,7 +661,7 @@ function writeUInt16BE(value, offset = 0) { return writeU_Int16BE(this, value, offset, 0, 0xffffffff); } -function writeIntLE(value, offset, byteLength) { +function writeIntLE(value, offset = 0, byteLength) { if (byteLength === 6) return writeU_Int48LE(this, value, offset, -0x800000000000, 0x7fffffffffff); if (byteLength === 5) @@ -680,7 +672,7 @@ function writeIntLE(value, offset, byteLength) { return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff); if (byteLength === 2) return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return writeU_Int8(this, value, offset, -0x80, 0x7f); boundsError(byteLength, 6, 'byteLength'); @@ -698,7 +690,7 @@ function writeInt8(value, offset = 0) { return writeU_Int8(this, value, offset, -0x80, 0x7f); } -function writeIntBE(value, offset, byteLength) { +function writeIntBE(value, offset = 0, byteLength) { if (byteLength === 6) return writeU_Int48BE(this, value, offset, -0x800000000000, 0x7fffffffffff); if (byteLength === 5) @@ -709,7 +701,7 @@ function writeIntBE(value, offset, byteLength) { return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff); if (byteLength === 2) return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff); - if (byteLength === 1) + if (byteLength === 1 || byteLength === undefined) return writeU_Int8(this, value, offset, -0x80, 0x7f); boundsError(byteLength, 6, 'byteLength'); diff --git a/test/parallel/test-buffer-readint.js b/test/parallel/test-buffer-readint.js index 3073ec595ada54..ce0e6681cec38b 100644 --- a/test/parallel/test-buffer-readint.js +++ b/test/parallel/test-buffer-readint.js @@ -134,7 +134,13 @@ const assert = require('assert'); // Check byteLength. ['readIntBE', 'readIntLE'].forEach((fn) => { - ['', '0', null, undefined, {}, [], () => {}, true, false].forEach((len) => { + + // Verify that default offset & byteLength works fine. + buffer[fn](undefined, undefined); + buffer[fn](undefined); + buffer[fn](); + + ['', '0', null, {}, [], () => {}, true, false].forEach((len) => { assert.throws( () => buffer[fn](0, len), { code: 'ERR_INVALID_ARG_TYPE' }); @@ -165,7 +171,7 @@ const assert = require('assert'); // Test 1 to 6 bytes. for (let i = 1; i < 6; i++) { ['readIntBE', 'readIntLE'].forEach((fn) => { - ['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => { + ['', '0', null, {}, [], () => {}, true, false].forEach((o) => { assert.throws( () => buffer[fn](o, i), { diff --git a/test/parallel/test-buffer-writeint.js b/test/parallel/test-buffer-writeint.js index 714cce60bb4c8a..8f167044e388f1 100644 --- a/test/parallel/test-buffer-writeint.js +++ b/test/parallel/test-buffer-writeint.js @@ -168,7 +168,13 @@ const errorOutOfBounds = common.expectsError({ // Check byteLength. ['writeIntBE', 'writeIntLE'].forEach((fn) => { - ['', '0', null, undefined, {}, [], () => {}, true, false].forEach((bl) => { + + // Verify that default offset & byteLength works fine. + data[fn](undefined, undefined); + data[fn](undefined); + data[fn](); + + ['', '0', null, {}, [], () => {}, true, false].forEach((bl) => { assert.throws( () => data[fn](23, 0, bl), { code: 'ERR_INVALID_ARG_TYPE' }); @@ -214,7 +220,7 @@ const errorOutOfBounds = common.expectsError({ }); }); - ['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => { + ['', '0', null, {}, [], () => {}, true, false].forEach((o) => { assert.throws( () => data[fn](min, o, i), { diff --git a/test/parallel/test-buffer-writeuint.js b/test/parallel/test-buffer-writeuint.js index d6c493b5448e90..1f304993ff9cb0 100644 --- a/test/parallel/test-buffer-writeuint.js +++ b/test/parallel/test-buffer-writeuint.js @@ -117,7 +117,13 @@ const assert = require('assert'); // Check byteLength. ['writeUIntBE', 'writeUIntLE'].forEach((fn) => { - ['', '0', null, undefined, {}, [], () => {}, true, false].forEach((bl) => { + + // Verify that default offset & byteLength works fine. + data[fn](undefined, undefined); + data[fn](undefined); + data[fn](); + + ['', '0', null, {}, [], () => {}, true, false].forEach((bl) => { assert.throws( () => data[fn](23, 0, bl), { code: 'ERR_INVALID_ARG_TYPE' }); @@ -158,7 +164,7 @@ const assert = require('assert'); `It must be >= 0 and <= ${val - 1}. Received ${val}` }); - ['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => { + ['', '0', null, {}, [], () => {}, true, false].forEach((o) => { assert.throws( () => data[fn](23, o, i), {