Skip to content

Commit

Permalink
console: fix console.dir crash on a revoked proxy
Browse files Browse the repository at this point in the history
Fixes: #43095

Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com

PR-URL: #43100
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
daeyeon authored and juanarbol committed May 31, 2022
1 parent 63045bf commit c4ff510
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/internal/util/inspect.js
Expand Up @@ -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('<Revoked Proxy>', 'special');
}
if (ctx.showProxy) {
return formatProxy(ctx, proxy, recurseTimes);
}
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 12 additions & 0 deletions 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 }));
21 changes: 21 additions & 0 deletions test/parallel/test-util-inspect-proxy.js
Expand Up @@ -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), '<Revoked Proxy>');
assert.strictEqual(
util.inspect(r, { showProxy: true }),
'{ proxy: <Revoked Proxy>, revoke: [Function (anonymous)] }',
);

assert.strictEqual(util.format('%s', r.proxy), '<Revoked Proxy>');

assert.strictEqual(
util.inspect(proxyObj, opts),
'Proxy [\n' +
Expand Down

0 comments on commit c4ff510

Please sign in to comment.