diff --git a/lib/buffer.js b/lib/buffer.js index 74d6511b35f59c..2bed199f5744dc 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -291,17 +291,21 @@ Buffer.from = function from(value, encodingOrOffset, length) { return fromArrayBuffer(value, encodingOrOffset, length); const valueOf = value.valueOf && value.valueOf(); - if (valueOf !== null && valueOf !== undefined && valueOf !== value) - return Buffer.from(valueOf, encodingOrOffset, length); + if (valueOf != null && + valueOf !== value && + (typeof valueOf === 'string' || typeof valueOf === 'object')) { + return from(valueOf, encodingOrOffset, length); + } const b = fromObject(value); if (b) return b; if (typeof value[SymbolToPrimitive] === 'function') { - return Buffer.from(value[SymbolToPrimitive]('string'), - encodingOrOffset, - length); + const primitive = value[SymbolToPrimitive]('string'); + if (typeof primitive === 'string') { + return fromString(primitive, encodingOrOffset); + } } }