diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 34786ab64024f7..d606ad430151c3 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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; @@ -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))) { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index e51b96fffd6d40..b14cab3d13358a 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -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;