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 420f885
Show file tree
Hide file tree
Showing 2 changed files with 24 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 formatValue(ctx, proxy, 0);
}
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
18 changes: 18 additions & 0 deletions test/parallel/test-console-issue-43095.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

require('../common');
const assert = require('node:assert');
const { inspect } = require('node:util');

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

console.dir(r.proxy);
console.dir(r);
console.log(r.proxy);

assert.strictEqual(inspect(r.proxy), 'null');
assert.strictEqual(
inspect(r.proxy, { showProxy: true }),
'Proxy [ null, null ]',
);

0 comments on commit 420f885

Please sign in to comment.