Skip to content

Commit f206505

Browse files
BridgeARBethGriggs
authored andcommitted
util: fix instanceof checks with null prototypes during inspection
Signed-off-by: Ruben Bridgewater <[email protected]> Backport-PR-URL: #37100 PR-URL: #36178 Fixes: #35730 Fixes: #36151 Refs: #35754 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2f7944b commit f206505

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/internal/util/inspect.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,14 @@ function getEmptyFormatArray() {
535535
return [];
536536
}
537537

538+
function isInstanceof(object, proto) {
539+
try {
540+
return object instanceof proto;
541+
} catch {
542+
return false;
543+
}
544+
}
545+
538546
function getConstructorName(obj, ctx, recurseTimes, protoProps) {
539547
let firstProto;
540548
const tmp = obj;
@@ -543,7 +551,7 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
543551
if (descriptor !== undefined &&
544552
typeof descriptor.value === 'function' &&
545553
descriptor.value.name !== '' &&
546-
tmp instanceof descriptor.value) {
554+
isInstanceof(tmp, descriptor.value)) {
547555
if (protoProps !== undefined &&
548556
(firstProto !== obj ||
549557
!builtInObjects.has(descriptor.value.name))) {

test/parallel/test-util-inspect.js

+11
Original file line numberDiff line numberDiff line change
@@ -2866,6 +2866,17 @@ assert.strictEqual(
28662866
);
28672867
}
28682868

2869+
// Check that prototypes with a null prototype are inspectable.
2870+
// Regression test for https://github.com/nodejs/node/issues/35730
2871+
{
2872+
function Func() {}
2873+
Func.prototype = null;
2874+
const object = {};
2875+
object.constructor = Func;
2876+
2877+
assert.strictEqual(util.inspect(object), '{ constructor: [Function: Func] }');
2878+
}
2879+
28692880
// Test changing util.inspect.colors colors and aliases.
28702881
{
28712882
const colors = util.inspect.colors;

0 commit comments

Comments
 (0)