Skip to content

Commit a049a90

Browse files
committed
resolve feedback
1 parent 48abfc5 commit a049a90

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
@@ -1200,6 +1200,7 @@ int32_t FastIndexOfNumber(Local<Value>,
12001200
static CFunction fast_index_of_number(CFunction::Make(FastIndexOfNumber));
12011201

12021202
void Swap16(const FunctionCallbackInfo<Value>& args) {
1203+
DCHECK(args[0]->IsArrayBufferView());
12031204
SPREAD_BUFFER_ARG(args[0], ts_obj);
12041205
CHECK(nbytes::SwapBytes16(ts_obj_data, ts_obj_length));
12051206
}
@@ -1217,6 +1218,7 @@ void FastSwap16(Local<Value> receiver,
12171218
static CFunction fast_swap16(CFunction::Make(FastSwap16));
12181219

12191220
void Swap32(const FunctionCallbackInfo<Value>& args) {
1221+
DCHECK(args[0]->IsArrayBufferView());
12201222
SPREAD_BUFFER_ARG(args[0], ts_obj);
12211223
CHECK(nbytes::SwapBytes32(ts_obj_data, ts_obj_length));
12221224
}
@@ -1234,6 +1236,7 @@ void FastSwap32(Local<Value> receiver,
12341236
static CFunction fast_swap32(CFunction::Make(FastSwap32));
12351237

12361238
void Swap64(const FunctionCallbackInfo<Value>& args) {
1239+
DCHECK(args[0]->IsArrayBufferView());
12371240
SPREAD_BUFFER_ARG(args[0], ts_obj);
12381241
CHECK(nbytes::SwapBytes64(ts_obj_data, ts_obj_length));
12391242
}

0 commit comments

Comments
 (0)