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: nodejs#43095

Signed-off-by: Daeyeon Jeong [email protected]
  • Loading branch information
daeyeon committed May 14, 2022
1 parent 556df3d commit 351ac8e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,9 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
if (proxy !== undefined) {
if (ctx.showProxy) {
return formatProxy(ctx, proxy, recurseTimes);
} else if (proxy === null) {
// The proxy is revoked.
return ctx.stylize('null', 'null');
}
value = proxy;
}
Expand Down Expand Up @@ -1966,9 +1969,10 @@ function hasBuiltInToString(value) {
// Prevent triggering proxy traps.
const getFullProxy = false;
const proxyTarget = getProxyDetails(value, getFullProxy);
if (proxyTarget !== undefined) {
value = proxyTarget;
if (proxyTarget === undefined || proxyTarget === null) {
return false;
}
value = proxyTarget;

// Count objects that have no `toString` function as built-in.
if (typeof value.toString !== 'function') {
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-console-issue-43095.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

require('../common');

const r = Proxy.revocable({}, {});
r.revoke();

console.dir(r);
console.dir(r.proxy);
console.log(r.proxy);
19 changes: 19 additions & 0 deletions test/parallel/test-util-inspect-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ 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), 'null');
assert.strictEqual(
util.inspect(r.proxy, { showProxy: true }),
'Proxy [ null, null ]',
);

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

0 comments on commit 351ac8e

Please sign in to comment.