util: fix instanceof checks with null prototypes during inspection

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

Fixes: https://github.com/nodejs/node/issues/35730

PR-URL: https://github.com/nodejs/node/pull/36178
Fixes: https://github.com/nodejs/node/issues/36151
Refs: https://github.com/nodejs/node/pull/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>
pull/36448/head
Ruben Bridgewater 2020-11-19 16:36:01 +01:00 committed by Node.js GitHub Bot
parent f78da00ab7
commit 1e665095c8
2 changed files with 20 additions and 1 deletions

View File

@ -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))) {

View File

@ -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;