Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: use a default offset #19749

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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