From cb9eec9f079949c99ac5f32bcd36b90dec07bd11 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 19 Nov 2020 16:36:01 +0100 Subject: [PATCH] util: fix instanceof checks with null prototypes during inspection Signed-off-by: Ruben Bridgewater Fixes: https://github.com/nodejs/node/issues/35730 --- lib/internal/util/inspect.js | 10 +++++++++- test/parallel/test-util-inspect.js | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index e6bba524c07288..bd7a17166d0ed1 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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; @@ -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))) { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 634f27690e5a59..56065beb458832 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;