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>
This commit is contained in:
Ruben Bridgewater authored and Michaël Zasso committed 2020-12-21 16:49:23 +01:00
1 parent 13d6597b4b
commit cc544dbfaa
2 files changed
+20 -1

No files matched your search

+9 -1
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))) {
+11
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;