Skip to content

Commit 728505a

Browse files
MnwaTrott
authored andcommitted
buffer: refactor checks for SlowBuffer creation
PR-URL: nodejs#25266 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0a549aa commit 728505a

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

lib/buffer.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const constants = Object.defineProperties({}, {
102102
});
103103

104104
Buffer.poolSize = 8 * 1024;
105-
var poolSize, poolOffset, allocPool;
105+
let poolSize, poolOffset, allocPool;
106106

107107
setupBufferJS(Buffer.prototype, bindingObj);
108108

@@ -221,7 +221,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
221221
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
222222
return Buffer.from(valueOf, encodingOrOffset, length);
223223

224-
var b = fromObject(value);
224+
const b = fromObject(value);
225225
if (b)
226226
return b;
227227

@@ -307,13 +307,10 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
307307
// If --zero-fill-buffers command line argument is set, a zero-filled
308308
// buffer is returned.
309309
function SlowBuffer(length) {
310-
const len = +length;
311-
// eslint-disable-next-line eqeqeq
312-
if (len != length)
313-
length = 0;
314-
else
315-
assertSize(len);
316-
return createUnsafeBuffer(len);
310+
if (typeof length !== 'number')
311+
length = +length;
312+
assertSize(length);
313+
return createUnsafeBuffer(length);
317314
}
318315

319316
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@@ -326,7 +323,7 @@ function allocate(size) {
326323
if (size < (Buffer.poolSize >>> 1)) {
327324
if (size > (poolSize - poolOffset))
328325
createPool();
329-
var b = new FastBuffer(allocPool, poolOffset, size);
326+
const b = new FastBuffer(allocPool, poolOffset, size);
330327
poolOffset += size;
331328
alignPool();
332329
return b;
@@ -335,7 +332,7 @@ function allocate(size) {
335332
}
336333

337334
function fromString(string, encoding) {
338-
var length;
335+
let length;
339336
if (typeof encoding !== 'string' || encoding.length === 0) {
340337
if (string.length === 0)
341338
return new FastBuffer();
@@ -354,7 +351,7 @@ function fromString(string, encoding) {
354351

355352
if (length > (poolSize - poolOffset))
356353
createPool();
357-
var b = new FastBuffer(allocPool, poolOffset, length);
354+
let b = new FastBuffer(allocPool, poolOffset, length);
358355
const actual = b.write(string, encoding);
359356
if (actual !== length) {
360357
// byteLength() may overestimate. That's a rare case, though.
@@ -456,7 +453,7 @@ Buffer.isEncoding = function isEncoding(encoding) {
456453
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
457454

458455
Buffer.concat = function concat(list, length) {
459-
var i;
456+
let i;
460457
if (!Array.isArray(list)) {
461458
throw new ERR_INVALID_ARG_TYPE(
462459
'list', ['Array', 'Buffer', 'Uint8Array'], list);
@@ -473,10 +470,10 @@ Buffer.concat = function concat(list, length) {
473470
length = length >>> 0;
474471
}
475472

476-
var buffer = Buffer.allocUnsafe(length);
477-
var pos = 0;
473+
const buffer = Buffer.allocUnsafe(length);
474+
let pos = 0;
478475
for (i = 0; i < list.length; i++) {
479-
var buf = list[i];
476+
const buf = list[i];
480477
if (!isUint8Array(buf)) {
481478
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
482479
// Instead, find the proper error code for this.
@@ -791,7 +788,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
791788
}
792789

793790
function slowIndexOf(buffer, val, byteOffset, encoding, dir) {
794-
var loweredCase = false;
791+
let loweredCase = false;
795792
for (;;) {
796793
switch (encoding) {
797794
case 'utf8':
@@ -926,7 +923,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
926923
length = undefined;
927924
}
928925

929-
var remaining = this.length - offset;
926+
const remaining = this.length - offset;
930927
if (length === undefined || length > remaining)
931928
length = remaining;
932929

0 commit comments

Comments
 (0)