From 255461e19ab08748e2b9b375ff26191bd1cdc8d9 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 21 Nov 2019 14:18:41 +0100 Subject: util: fix inspection of errors with tampered name or stack property This makes sure that `util.inspect()` does not throw while inspecting errors that have the name or stack property set to a different type than string. Fixes: https://github.com/nodejs/node/issues/30572 PR-URL: https://github.com/nodejs/node/pull/30576 Reviewed-By: David Carlier Reviewed-By: Anto Aravinth Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- lib/internal/util/inspect.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/internal/util/inspect.js') diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 89237a09a5..9677dfb226 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -937,12 +937,12 @@ function getFunctionBase(value, constructor, tag) { } function formatError(err, constructor, tag, ctx) { - let stack = err.stack || ErrorPrototypeToString(err); + const name = err.name != null ? String(err.name) : 'Error'; + let len = name.length; + let stack = err.stack ? String(err.stack) : ErrorPrototypeToString(err); // A stack trace may contain arbitrary data. Only manipulate the output // for "regular errors" (errors that "look normal") for now. - const name = err.name || 'Error'; - let len = name.length; if (constructor === null || (name.endsWith('Error') && stack.startsWith(name) && -- cgit v1.2.3