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

PR-URL: #36178
Fixes: #36151
Refs: #35754
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Dec 21, 2020
1 parent 13d6597 commit cc544db
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
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,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 @@ -535,7 +543,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
Original file line number Diff line number Diff line change
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 cc544db

Please sign in to comment.