Skip to content

Commit cb9eec9

Browse files
committed
util: fix instanceof checks with null prototypes during inspection
Signed-off-by: Ruben Bridgewater <[email protected]> Fixes: #35730
1 parent 6bf0b56 commit cb9eec9

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
@@ -534,6 +534,14 @@ function getEmptyFormatArray() {
534534
return [];
535535
}
536536

537+
function isInstanceof(object, proto) {
538+
try {
539+
return object instanceof proto;
540+
} catch {
541+
return false;
542+
}
543+
}
544+
537545
function getConstructorName(obj, ctx, recurseTimes, protoProps) {
538546
let firstProto;
539547
const tmp = obj;
@@ -542,7 +550,7 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
542550
if (descriptor !== undefined &&
543551
typeof descriptor.value === 'function' &&
544552
descriptor.value.name !== '' &&
545-
tmp instanceof descriptor.value) {
553+
isInstanceof(tmp, descriptor.value)) {
546554
if (protoProps !== undefined &&
547555
(firstProto !== obj ||
548556
!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)