Skip to content

Commit 2a67802

Browse files
committed
resolve feedback
1 parent 6e9f17d commit 2a67802

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

lib/buffer.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,13 +1267,15 @@ function swap(b, n, m) {
12671267
}
12681268

12691269
Buffer.prototype.swap16 = function swap16() {
1270-
// For Buffer.length < 128, it's generally faster to
1270+
// For Buffer.length <= 32, it's generally faster to
12711271
// do the swap in javascript. For larger buffers,
12721272
// dropping down to the native code is faster.
1273+
if (!isUint8Array(this))
1274+
throw new ERR_INVALID_ARG_TYPE('this', ['Buffer', 'Uint8Array'], this);
12731275
const len = this.length;
12741276
if (len % 2 !== 0)
12751277
throw new ERR_INVALID_BUFFER_SIZE('16-bits');
1276-
if (len < 128) {
1278+
if (len <= 32) {
12771279
for (let i = 0; i < len; i += 2)
12781280
swap(this, i, i + 1);
12791281
return this;
@@ -1283,13 +1285,15 @@ Buffer.prototype.swap16 = function swap16() {
12831285
};
12841286

12851287
Buffer.prototype.swap32 = function swap32() {
1286-
// For Buffer.length < 192, it's generally faster to
1288+
// For Buffer.length <= 32, it's generally faster to
12871289
// do the swap in javascript. For larger buffers,
12881290
// dropping down to the native code is faster.
1291+
if (!isUint8Array(this))
1292+
throw new ERR_INVALID_ARG_TYPE('this', ['Buffer', 'Uint8Array'], this);
12891293
const len = this.length;
12901294
if (len % 4 !== 0)
12911295
throw new ERR_INVALID_BUFFER_SIZE('32-bits');
1292-
if (len < 192) {
1296+
if (len <= 32) {
12931297
for (let i = 0; i < len; i += 4) {
12941298
swap(this, i, i + 3);
12951299
swap(this, i + 1, i + 2);
@@ -1301,13 +1305,15 @@ Buffer.prototype.swap32 = function swap32() {
13011305
};
13021306

13031307
Buffer.prototype.swap64 = function swap64() {
1304-
// For Buffer.length < 192, it's generally faster to
1308+
// For Buffer.length < 48, it's generally faster to
13051309
// do the swap in javascript. For larger buffers,
13061310
// dropping down to the native code is faster.
1311+
if (!isUint8Array(this))
1312+
throw new ERR_INVALID_ARG_TYPE('this', ['Buffer', 'Uint8Array'], this);
13071313
const len = this.length;
13081314
if (len % 8 !== 0)
13091315
throw new ERR_INVALID_BUFFER_SIZE('64-bits');
1310-
if (len < 192) {
1316+
if (len < 48) {
13111317
for (let i = 0; i < len; i += 8) {
13121318
swap(this, i, i + 7);
13131319
swap(this, i + 1, i + 6);

src/node_buffer.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,7 @@ int32_t FastIndexOfNumber(Local<Value>,
11961196
static CFunction fast_index_of_number(CFunction::Make(FastIndexOfNumber));
11971197

11981198
void Swap16(const FunctionCallbackInfo<Value>& args) {
1199+
DCHECK(args[0]->IsArrayBufferView());
11991200
SPREAD_BUFFER_ARG(args[0], ts_obj);
12001201
CHECK(nbytes::SwapBytes16(ts_obj_data, ts_obj_length));
12011202
}
@@ -1213,6 +1214,7 @@ void FastSwap16(Local<Value> receiver,
12131214
static CFunction fast_swap16(CFunction::Make(FastSwap16));
12141215

12151216
void Swap32(const FunctionCallbackInfo<Value>& args) {
1217+
DCHECK(args[0]->IsArrayBufferView());
12161218
SPREAD_BUFFER_ARG(args[0], ts_obj);
12171219
CHECK(nbytes::SwapBytes32(ts_obj_data, ts_obj_length));
12181220
}
@@ -1230,6 +1232,7 @@ void FastSwap32(Local<Value> receiver,
12301232
static CFunction fast_swap32(CFunction::Make(FastSwap32));
12311233

12321234
void Swap64(const FunctionCallbackInfo<Value>& args) {
1235+
DCHECK(args[0]->IsArrayBufferView());
12331236
SPREAD_BUFFER_ARG(args[0], ts_obj);
12341237
CHECK(nbytes::SwapBytes64(ts_obj_data, ts_obj_length));
12351238
}

0 commit comments

Comments
 (0)