Skip to content

Commit

Permalink
util: fix instanceof checks with null prototypes during inspection
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: #35730
  • Loading branch information
BridgeAR committed Nov 19, 2020
1 parent 6bf0b56 commit cb9eec9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/internal/util/inspect.js
Expand Up @@ -534,6 +534,14 @@ function getEmptyFormatArray() {
return [];
}

function isInstanceof(object, proto) {
try {
return object instanceof proto;
} catch {
return false;
}
}

function getConstructorName(obj, ctx, recurseTimes, protoProps) {
let firstProto;
const tmp = obj;
Expand All @@ -542,7 +550,7 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
if (descriptor !== undefined &&
typeof descriptor.value === 'function' &&
descriptor.value.name !== '' &&
tmp instanceof descriptor.value) {
isInstanceof(tmp, descriptor.value)) {
if (protoProps !== undefined &&
(firstProto !== obj ||
!builtInObjects.has(descriptor.value.name))) {
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-util-inspect.js
Expand Up @@ -2866,6 +2866,17 @@ assert.strictEqual(
);
}

// Check that prototypes with a null prototype are inspectable.
// Regression test for https://github.com/nodejs/node/issues/35730
{
function Func() {}
Func.prototype = null;
const object = {};
object.constructor = Func;

assert.strictEqual(util.inspect(object), '{ constructor: [Function: Func] }');
}

// Test changing util.inspect.colors colors and aliases.
{
const colors = util.inspect.colors;
Expand Down

0 comments on commit cb9eec9

Please sign in to comment.