Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup: accept weird cases as well
  • Loading branch information
BridgeAR committed Apr 10, 2018
commit 12e344a0dee42aba62d13b76de53844887c9dfdf
32 changes: 12 additions & 20 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand All @@ -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');
Expand All @@ -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)
Expand All @@ -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');
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-buffer-readint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -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),
{
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-buffer-writeint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -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),
{
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-buffer-writeuint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -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),
{
Expand Down