From 236445bc8a23025d1af648a13684cad644d3ea92 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Sun, 22 May 2022 19:41:55 +0900 Subject: [PATCH] console: fix console.dir crash on a revoked proxy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/43095 Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: https://github.com/nodejs/node/pull/43100 Reviewed-By: Ruben Bridgewater Reviewed-By: Michaƫl Zasso Reviewed-By: Antoine du Hamel --- lib/internal/util/inspect.js | 6 ++++++ test/parallel/test-console-issue-43095.js | 12 ++++++++++++ test/parallel/test-util-inspect-proxy.js | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 test/parallel/test-console-issue-43095.js diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index d78a4e97d21..7285014e803 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -759,6 +759,9 @@ function formatValue(ctx, value, recurseTimes, typedArray) { // any proxy handlers. const proxy = getProxyDetails(value, !!ctx.showProxy); if (proxy !== undefined) { + if (proxy === null || proxy[0] === null) { + return ctx.stylize('', 'special'); + } if (ctx.showProxy) { return formatProxy(ctx, proxy, recurseTimes); } @@ -1967,6 +1970,9 @@ function hasBuiltInToString(value) { const getFullProxy = false; const proxyTarget = getProxyDetails(value, getFullProxy); if (proxyTarget !== undefined) { + if (proxyTarget === null) { + return true; + } value = proxyTarget; } diff --git a/test/parallel/test-console-issue-43095.js b/test/parallel/test-console-issue-43095.js new file mode 100644 index 00000000000..647f4af2df4 --- /dev/null +++ b/test/parallel/test-console-issue-43095.js @@ -0,0 +1,12 @@ +'use strict'; + +require('../common'); +const { inspect } = require('node:util'); + +const r = Proxy.revocable({}, {}); +r.revoke(); + +console.dir(r); +console.dir(r.proxy); +console.log(r.proxy); +console.log(inspect(r.proxy, { showProxy: true })); diff --git a/test/parallel/test-util-inspect-proxy.js b/test/parallel/test-util-inspect-proxy.js index 3e1341fadfc..6344adae990 100644 --- a/test/parallel/test-util-inspect-proxy.js +++ b/test/parallel/test-util-inspect-proxy.js @@ -57,6 +57,27 @@ assert.strictEqual(handler, details[1]); details = processUtil.getProxyDetails(proxyObj, false); assert.strictEqual(target, details); +details = processUtil.getProxyDetails({}, true); +assert.strictEqual(details, undefined); + +const r = Proxy.revocable({}, {}); +r.revoke(); + +details = processUtil.getProxyDetails(r.proxy, true); +assert.strictEqual(details[0], null); +assert.strictEqual(details[1], null); + +details = processUtil.getProxyDetails(r.proxy, false); +assert.strictEqual(details, null); + +assert.strictEqual(util.inspect(r.proxy), ''); +assert.strictEqual( + util.inspect(r, { showProxy: true }), + '{ proxy: , revoke: [Function (anonymous)] }', +); + +assert.strictEqual(util.format('%s', r.proxy), ''); + assert.strictEqual( util.inspect(proxyObj, opts), 'Proxy [\n' +