Skip to content

Commit 1d49408

Browse files
Mnwaaddaleax
authored andcommitted
buffer: refactor checks for SlowBuffer creation
PR-URL: #25266 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent dd8795f commit 1d49408

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

lib/buffer.js

+15-18
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const constants = Object.defineProperties({}, {
9393
});
9494

9595
Buffer.poolSize = 8 * 1024;
96-
var poolSize, poolOffset, allocPool;
96+
let poolSize, poolOffset, allocPool;
9797

9898
setupBufferJS(Buffer.prototype, bindingObj);
9999

@@ -212,7 +212,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
212212
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
213213
return Buffer.from(valueOf, encodingOrOffset, length);
214214

215-
var b = fromObject(value);
215+
const b = fromObject(value);
216216
if (b)
217217
return b;
218218

@@ -298,13 +298,10 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) {
298298
// If --zero-fill-buffers command line argument is set, a zero-filled
299299
// buffer is returned.
300300
function SlowBuffer(length) {
301-
const len = +length;
302-
// eslint-disable-next-line eqeqeq
303-
if (len != length)
304-
length = 0;
305-
else
306-
assertSize(len);
307-
return createUnsafeBuffer(len);
301+
if (typeof length !== 'number')
302+
length = +length;
303+
assertSize(length);
304+
return createUnsafeBuffer(length);
308305
}
309306

310307
Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype);
@@ -317,7 +314,7 @@ function allocate(size) {
317314
if (size < (Buffer.poolSize >>> 1)) {
318315
if (size > (poolSize - poolOffset))
319316
createPool();
320-
var b = new FastBuffer(allocPool, poolOffset, size);
317+
const b = new FastBuffer(allocPool, poolOffset, size);
321318
poolOffset += size;
322319
alignPool();
323320
return b;
@@ -326,7 +323,7 @@ function allocate(size) {
326323
}
327324

328325
function fromString(string, encoding) {
329-
var length;
326+
let length;
330327
if (typeof encoding !== 'string' || encoding.length === 0) {
331328
if (string.length === 0)
332329
return new FastBuffer();
@@ -345,7 +342,7 @@ function fromString(string, encoding) {
345342

346343
if (length > (poolSize - poolOffset))
347344
createPool();
348-
var b = new FastBuffer(allocPool, poolOffset, length);
345+
let b = new FastBuffer(allocPool, poolOffset, length);
349346
const actual = b.write(string, encoding);
350347
if (actual !== length) {
351348
// byteLength() may overestimate. That's a rare case, though.
@@ -447,7 +444,7 @@ Buffer.isEncoding = function isEncoding(encoding) {
447444
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
448445

449446
Buffer.concat = function concat(list, length) {
450-
var i;
447+
let i;
451448
if (!Array.isArray(list)) {
452449
throw new ERR_INVALID_ARG_TYPE(
453450
'list', ['Array', 'Buffer', 'Uint8Array'], list);
@@ -464,10 +461,10 @@ Buffer.concat = function concat(list, length) {
464461
length = length >>> 0;
465462
}
466463

467-
var buffer = Buffer.allocUnsafe(length);
468-
var pos = 0;
464+
const buffer = Buffer.allocUnsafe(length);
465+
let pos = 0;
469466
for (i = 0; i < list.length; i++) {
470-
var buf = list[i];
467+
const buf = list[i];
471468
if (!isUint8Array(buf)) {
472469
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
473470
// Instead, find the proper error code for this.
@@ -772,7 +769,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
772769
}
773770

774771
function slowIndexOf(buffer, val, byteOffset, encoding, dir) {
775-
var loweredCase = false;
772+
let loweredCase = false;
776773
for (;;) {
777774
switch (encoding) {
778775
case 'utf8':
@@ -907,7 +904,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
907904
length = undefined;
908905
}
909906

910-
var remaining = this.length - offset;
907+
const remaining = this.length - offset;
911908
if (length === undefined || length > remaining)
912909
length = remaining;
913910

0 commit comments

Comments
 (0)