From ae3459af9f1a1026ba25b7cde3a80357c73d6d39 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 23 Sep 2019 08:14:35 +0200 Subject: [PATCH] buffer: improve .from() error details This makes sure the original input is passed to the error in case no matching inputs are found. Instead of passing along all values, only valid or possibliy valid values are passed through. That way invalid values end up in the error case with the original input. PR-URL: https://github.com/nodejs/node/pull/29675 Reviewed-By: Rich Trott --- lib/buffer.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 615d192ce43014..01b9ea97f95a68 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -292,17 +292,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); + } } }