From d33853ac3357eb8829eac62d91325e22475cbc91 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 12 Jan 2025 13:15:27 -0800 Subject: [PATCH] util: inspect: do not crash on an Error with a regex `name` See #56570 --- lib/internal/util/inspect.js | 2 +- test/parallel/test-util-inspect.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index cc76bbeed9bc11..9224f0956b1342 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1354,7 +1354,7 @@ function removeDuplicateErrorKeys(ctx, keys, err, stack) { for (const name of ['name', 'message', 'stack']) { const index = ArrayPrototypeIndexOf(keys, name); // Only hide the property in case it's part of the original stack - if (index !== -1 && StringPrototypeIncludes(stack, err[name])) { + if (index !== -1 && StringPrototypeIncludes(stack, String(err[name]))) { ArrayPrototypeSplice(keys, index, 1); } } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 94064d9491894c..4b4497293d5675 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -3426,3 +3426,14 @@ assert.strictEqual( Object.defineProperty(BuiltinPrototype, 'constructor', desc); } } + +{ + const error = new Error(); + const re = /a/g; + error.name = re; + assert.strictEqual(error.name, re); + assert.strictEqual( + util.inspect(error), + error.stack, + ); +}