diff --git a/lib/buffer.js b/lib/buffer.js index b032736f509e5c..549a459335c1d1 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -298,11 +298,13 @@ Buffer.allocUnsafeSlow = function allocUnsafeSlow(size) { // If --zero-fill-buffers command line argument is set, a zero-filled // buffer is returned. function SlowBuffer(length) { + const len = +length; // eslint-disable-next-line eqeqeq - if (+length != length) + if (len != length) length = 0; - assertSize(+length); - return createUnsafeBuffer(+length); + else + assertSize(len); + return createUnsafeBuffer(len); } Object.setPrototypeOf(SlowBuffer.prototype, Uint8Array.prototype); @@ -319,9 +321,8 @@ function allocate(size) { poolOffset += size; alignPool(); return b; - } else { - return createUnsafeBuffer(size); } + return createUnsafeBuffer(size); } function fromString(string, encoding) { @@ -639,21 +640,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) { } const len = this.length; - if (len === 0) - return ''; - if (!start || start < 0) + if (start <= 0) start = 0; else if (start >= len) return ''; + else + start |= 0; if (end === undefined || end > len) end = len; - else if (end <= 0) - return ''; - - start |= 0; - end |= 0; + else + end |= 0; if (end <= start) return ''; @@ -672,10 +670,10 @@ Buffer.prototype.equals = function equals(otherBuffer) { }; // Override how buffers are presented by util.inspect(). -Buffer.prototype[customInspectSymbol] = function inspect() { - var str = ''; - var max = exports.INSPECT_MAX_BYTES; - str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim(); +Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) { + const max = exports.INSPECT_MAX_BYTES; + const actualMax = Math.min(max, this.length); + let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim(); const remaining = this.length - max; if (remaining > 0) str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`; @@ -977,9 +975,8 @@ Buffer.prototype.toJSON = function toJSON() { for (var i = 0; i < this.length; ++i) data[i] = this[i]; return { type: 'Buffer', data }; - } else { - return { type: 'Buffer', data: [] }; } + return { type: 'Buffer', data: [] }; }; function adjustOffset(offset, length) {