Skip to content

Commit cc544db

Browse files
BridgeARtargos
authored andcommitted
util: fix instanceof checks with null prototypes during inspection
Signed-off-by: Ruben Bridgewater <[email protected]> Fixes: #35730 PR-URL: #36178 Fixes: #36151 Refs: #35754 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 13d6597 commit cc544db

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
@@ -527,6 +527,14 @@ function getEmptyFormatArray() {
527527
return [];
528528
}
529529

530+
function isInstanceof(object, proto) {
531+
try {
532+
return object instanceof proto;
533+
} catch {
534+
return false;
535+
}
536+
}
537+
530538
function getConstructorName(obj, ctx, recurseTimes, protoProps) {
531539
let firstProto;
532540
const tmp = obj;
@@ -535,7 +543,7 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
535543
if (descriptor !== undefined &&
536544
typeof descriptor.value === 'function' &&
537545
descriptor.value.name !== '' &&
538-
tmp instanceof descriptor.value) {
546+
isInstanceof(tmp, descriptor.value)) {
539547
if (protoProps !== undefined &&
540548
(firstProto !== obj ||
541549
!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)