Skip to content

Commit

Permalink
buffer: use a default offset
Browse files Browse the repository at this point in the history
If none is provided, use zero as a default offset for all read/write
operations on the buffer.

PR-URL: #19749
Refs: #18395
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
BridgeAR committed Apr 13, 2018
1 parent a25f567 commit d5495e8
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 70 deletions.
108 changes: 55 additions & 53 deletions lib/internal/buffer.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion test/parallel/test-buffer-readdouble.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ assert.strictEqual(buffer.readDoubleBE(0), 3.04814e-319);
assert.strictEqual(buffer.readDoubleLE(0), -Infinity);

['readDoubleLE', 'readDoubleBE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {

// Verify that default offset works fine.
buffer[fn](undefined);
buffer[fn]();

['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](off),
{ code: 'ERR_INVALID_ARG_TYPE' }
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-buffer-readfloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ assert.strictEqual(buffer.readFloatBE(0), 4.627507918739843e-41);
assert.strictEqual(buffer.readFloatLE(0), -Infinity);

['readFloatLE', 'readFloatBE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {

// Verify that default offset works fine.
buffer[fn](undefined);
buffer[fn]();

['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](off),
{ code: 'ERR_INVALID_ARG_TYPE' }
Expand Down
17 changes: 14 additions & 3 deletions test/parallel/test-buffer-readint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const assert = require('assert');
const buffer = Buffer.alloc(4);

['Int8', 'Int16BE', 'Int16LE', 'Int32BE', 'Int32LE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {

// Verify that default offset works fine.
buffer[`read${fn}`](undefined);
buffer[`read${fn}`]();

['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => buffer[`read${fn}`](o),
{
Expand Down Expand Up @@ -129,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 @@ -160,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
7 changes: 6 additions & 1 deletion test/parallel/test-buffer-writedouble.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ assert.ok(Number.isNaN(buffer.readDoubleLE(8)));
const small = Buffer.allocUnsafe(1);

['writeDoubleLE', 'writeDoubleBE'].forEach((fn) => {

// Verify that default offset works fine.
buffer[fn](23, undefined);
buffer[fn](23);

assert.throws(
() => small[fn](11.11, 0),
{
Expand All @@ -88,7 +93,7 @@ assert.ok(Number.isNaN(buffer.readDoubleLE(8)));
message: 'Attempt to write outside buffer bounds'
});

['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => small[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-buffer-writefloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ assert.ok(Number.isNaN(buffer.readFloatLE(4)));
const small = Buffer.allocUnsafe(1);

['writeFloatLE', 'writeFloatBE'].forEach((fn) => {

// Verify that default offset works fine.
buffer[fn](23, undefined);
buffer[fn](23);

assert.throws(
() => small[fn](11.11, 0),
{
Expand All @@ -69,7 +74,7 @@ assert.ok(Number.isNaN(buffer.readFloatLE(4)));
message: 'Attempt to write outside buffer bounds'
});

['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => small[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' }
Expand Down
32 changes: 26 additions & 6 deletions test/parallel/test-buffer-writeint.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ const errorOutOfBounds = common.expectsError({
buffer.writeInt8(-0x80 - 1, 0);
}, errorOutOfBounds);

['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
// Verify that default offset works fine.
buffer.writeInt8(23, undefined);
buffer.writeInt8(23);

['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer.writeInt8(23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
Expand Down Expand Up @@ -70,14 +74,19 @@ const errorOutOfBounds = common.expectsError({
assert.ok(buffer.equals(new Uint8Array([ 0xff, 0x7f, 0x00, 0x80 ])));

['writeInt16BE', 'writeInt16LE'].forEach((fn) => {

// Verify that default offset works fine.
buffer[fn](23, undefined);
buffer[fn](23);

assert.throws(() => {
buffer[fn](0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer[fn](-0x8000 - 1, 0);
}, errorOutOfBounds);

['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
Expand Down Expand Up @@ -127,14 +136,19 @@ const errorOutOfBounds = common.expectsError({
])));

['writeInt32BE', 'writeInt32LE'].forEach((fn) => {

// Verify that default offset works fine.
buffer[fn](23, undefined);
buffer[fn](23);

assert.throws(() => {
buffer[fn](0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer[fn](-0x80000000 - 1, 0);
}, errorOutOfBounds);

['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
Expand All @@ -154,9 +168,15 @@ const errorOutOfBounds = common.expectsError({

// Check byteLength.
['writeIntBE', 'writeIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {

// 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, o),
() => data[fn](23, 0, bl),
{ code: 'ERR_INVALID_ARG_TYPE' });
});

Expand Down Expand Up @@ -200,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
19 changes: 15 additions & 4 deletions test/parallel/test-buffer-writeuint.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const assert = require('assert');
{ // OOB
const data = Buffer.alloc(8);
['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {

// Verify that default offset works fine.
data[`write${fn}`](23, undefined);
data[`write${fn}`](23);

['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[`write${fn}`](23, o),
{ code: 'ERR_INVALID_ARG_TYPE' });
Expand Down Expand Up @@ -112,9 +117,15 @@ const assert = require('assert');

// Check byteLength.
['writeUIntBE', 'writeUIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {

// 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, o),
() => data[fn](23, 0, bl),
{ code: 'ERR_INVALID_ARG_TYPE' });
});

Expand Down Expand Up @@ -153,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

0 comments on commit d5495e8

Please sign in to comment.