Skip to content

Commit cdacafc

Browse files
BridgeARjasnell
authored andcommitted
buffer: use a default offset
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 <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1964978 commit cdacafc

8 files changed

+134
-70
lines changed

lib/internal/buffer.js

Lines changed: 55 additions & 53 deletions
Large diffs are not rendered by default.

test/parallel/test-buffer-readdouble.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ assert.strictEqual(buffer.readDoubleBE(0), 3.04814e-319);
9999
assert.strictEqual(buffer.readDoubleLE(0), -Infinity);
100100

101101
['readDoubleLE', 'readDoubleBE'].forEach((fn) => {
102-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
102+
103+
// Verify that default offset works fine.
104+
buffer[fn](undefined);
105+
buffer[fn]();
106+
107+
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
103108
assert.throws(
104109
() => buffer[fn](off),
105110
{ code: 'ERR_INVALID_ARG_TYPE' }

test/parallel/test-buffer-readfloat.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ assert.strictEqual(buffer.readFloatBE(0), 4.627507918739843e-41);
6262
assert.strictEqual(buffer.readFloatLE(0), -Infinity);
6363

6464
['readFloatLE', 'readFloatBE'].forEach((fn) => {
65-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
65+
66+
// Verify that default offset works fine.
67+
buffer[fn](undefined);
68+
buffer[fn]();
69+
70+
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
6671
assert.throws(
6772
() => buffer[fn](off),
6873
{ code: 'ERR_INVALID_ARG_TYPE' }

test/parallel/test-buffer-readint.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ const assert = require('assert');
88
const buffer = Buffer.alloc(4);
99

1010
['Int8', 'Int16BE', 'Int16LE', 'Int32BE', 'Int32LE'].forEach((fn) => {
11-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
11+
12+
// Verify that default offset works fine.
13+
buffer[`read${fn}`](undefined);
14+
buffer[`read${fn}`]();
15+
16+
['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
1217
assert.throws(
1318
() => buffer[`read${fn}`](o),
1419
{
@@ -129,7 +134,13 @@ const assert = require('assert');
129134

130135
// Check byteLength.
131136
['readIntBE', 'readIntLE'].forEach((fn) => {
132-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((len) => {
137+
138+
// Verify that default offset & byteLength works fine.
139+
buffer[fn](undefined, undefined);
140+
buffer[fn](undefined);
141+
buffer[fn]();
142+
143+
['', '0', null, {}, [], () => {}, true, false].forEach((len) => {
133144
assert.throws(
134145
() => buffer[fn](0, len),
135146
{ code: 'ERR_INVALID_ARG_TYPE' });
@@ -160,7 +171,7 @@ const assert = require('assert');
160171
// Test 1 to 6 bytes.
161172
for (let i = 1; i < 6; i++) {
162173
['readIntBE', 'readIntLE'].forEach((fn) => {
163-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
174+
['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
164175
assert.throws(
165176
() => buffer[fn](o, i),
166177
{

test/parallel/test-buffer-writedouble.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ assert.ok(Number.isNaN(buffer.readDoubleLE(8)));
8080
const small = Buffer.allocUnsafe(1);
8181

8282
['writeDoubleLE', 'writeDoubleBE'].forEach((fn) => {
83+
84+
// Verify that default offset works fine.
85+
buffer[fn](23, undefined);
86+
buffer[fn](23);
87+
8388
assert.throws(
8489
() => small[fn](11.11, 0),
8590
{
@@ -88,7 +93,7 @@ assert.ok(Number.isNaN(buffer.readDoubleLE(8)));
8893
message: 'Attempt to write outside buffer bounds'
8994
});
9095

91-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
96+
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
9297
assert.throws(
9398
() => small[fn](23, off),
9499
{ code: 'ERR_INVALID_ARG_TYPE' });

test/parallel/test-buffer-writefloat.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ assert.ok(Number.isNaN(buffer.readFloatLE(4)));
6161
const small = Buffer.allocUnsafe(1);
6262

6363
['writeFloatLE', 'writeFloatBE'].forEach((fn) => {
64+
65+
// Verify that default offset works fine.
66+
buffer[fn](23, undefined);
67+
buffer[fn](23);
68+
6469
assert.throws(
6570
() => small[fn](11.11, 0),
6671
{
@@ -69,7 +74,7 @@ assert.ok(Number.isNaN(buffer.readFloatLE(4)));
6974
message: 'Attempt to write outside buffer bounds'
7075
});
7176

72-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
77+
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
7378
assert.throws(
7479
() => small[fn](23, off),
7580
{ code: 'ERR_INVALID_ARG_TYPE' }

test/parallel/test-buffer-writeint.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ const errorOutOfBounds = common.expectsError({
3131
buffer.writeInt8(-0x80 - 1, 0);
3232
}, errorOutOfBounds);
3333

34-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
34+
// Verify that default offset works fine.
35+
buffer.writeInt8(23, undefined);
36+
buffer.writeInt8(23);
37+
38+
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
3539
assert.throws(
3640
() => buffer.writeInt8(23, off),
3741
{ code: 'ERR_INVALID_ARG_TYPE' });
@@ -70,14 +74,19 @@ const errorOutOfBounds = common.expectsError({
7074
assert.ok(buffer.equals(new Uint8Array([ 0xff, 0x7f, 0x00, 0x80 ])));
7175

7276
['writeInt16BE', 'writeInt16LE'].forEach((fn) => {
77+
78+
// Verify that default offset works fine.
79+
buffer[fn](23, undefined);
80+
buffer[fn](23);
81+
7382
assert.throws(() => {
7483
buffer[fn](0x7fff + 1, 0);
7584
}, errorOutOfBounds);
7685
assert.throws(() => {
7786
buffer[fn](-0x8000 - 1, 0);
7887
}, errorOutOfBounds);
7988

80-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
89+
['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
8190
assert.throws(
8291
() => buffer[fn](23, off),
8392
{ code: 'ERR_INVALID_ARG_TYPE' });
@@ -127,14 +136,19 @@ const errorOutOfBounds = common.expectsError({
127136
])));
128137

129138
['writeInt32BE', 'writeInt32LE'].forEach((fn) => {
139+
140+
// Verify that default offset works fine.
141+
buffer[fn](23, undefined);
142+
buffer[fn](23);
143+
130144
assert.throws(() => {
131145
buffer[fn](0x7fffffff + 1, 0);
132146
}, errorOutOfBounds);
133147
assert.throws(() => {
134148
buffer[fn](-0x80000000 - 1, 0);
135149
}, errorOutOfBounds);
136150

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

155169
// Check byteLength.
156170
['writeIntBE', 'writeIntLE'].forEach((fn) => {
157-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
171+
172+
// Verify that default offset & byteLength works fine.
173+
data[fn](undefined, undefined);
174+
data[fn](undefined);
175+
data[fn]();
176+
177+
['', '0', null, {}, [], () => {}, true, false].forEach((bl) => {
158178
assert.throws(
159-
() => data[fn](23, 0, o),
179+
() => data[fn](23, 0, bl),
160180
{ code: 'ERR_INVALID_ARG_TYPE' });
161181
});
162182

@@ -200,7 +220,7 @@ const errorOutOfBounds = common.expectsError({
200220
});
201221
});
202222

203-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
223+
['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
204224
assert.throws(
205225
() => data[fn](min, o, i),
206226
{

test/parallel/test-buffer-writeuint.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ const assert = require('assert');
1414
{ // OOB
1515
const data = Buffer.alloc(8);
1616
['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => {
17-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
17+
18+
// Verify that default offset works fine.
19+
data[`write${fn}`](23, undefined);
20+
data[`write${fn}`](23);
21+
22+
['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
1823
assert.throws(
1924
() => data[`write${fn}`](23, o),
2025
{ code: 'ERR_INVALID_ARG_TYPE' });
@@ -112,9 +117,15 @@ const assert = require('assert');
112117

113118
// Check byteLength.
114119
['writeUIntBE', 'writeUIntLE'].forEach((fn) => {
115-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
120+
121+
// Verify that default offset & byteLength works fine.
122+
data[fn](undefined, undefined);
123+
data[fn](undefined);
124+
data[fn]();
125+
126+
['', '0', null, {}, [], () => {}, true, false].forEach((bl) => {
116127
assert.throws(
117-
() => data[fn](23, 0, o),
128+
() => data[fn](23, 0, bl),
118129
{ code: 'ERR_INVALID_ARG_TYPE' });
119130
});
120131

@@ -153,7 +164,7 @@ const assert = require('assert');
153164
`It must be >= 0 and <= ${val - 1}. Received ${val}`
154165
});
155166

156-
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
167+
['', '0', null, {}, [], () => {}, true, false].forEach((o) => {
157168
assert.throws(
158169
() => data[fn](23, o, i),
159170
{

0 commit comments

Comments
 (0)