Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f206505

Browse filesBrowse files
BridgeARBethGriggs
authored andcommitted
util: fix instanceof checks with null prototypes during inspection
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> Backport-PR-URL: #37100 PR-URL: #36178 Fixes: #35730 Fixes: #36151 Refs: #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>
1 parent 2f7944b commit f206505
Copy full SHA for f206505

File tree

Expand file treeCollapse file tree

2 files changed

+20
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+20
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,14 @@ function getEmptyFormatArray() {
535535
return [];
536536
}
537537

538+
function isInstanceof(object, proto) {
539+
try {
540+
return object instanceof proto;
541+
} catch {
542+
return false;
543+
}
544+
}
545+
538546
function getConstructorName(obj, ctx, recurseTimes, protoProps) {
539547
let firstProto;
540548
const tmp = obj;
@@ -543,7 +551,7 @@ function getConstructorName(obj, ctx, recurseTimes, protoProps) {
543551
if (descriptor !== undefined &&
544552
typeof descriptor.value === 'function' &&
545553
descriptor.value.name !== '' &&
546-
tmp instanceof descriptor.value) {
554+
isInstanceof(tmp, descriptor.value)) {
547555
if (protoProps !== undefined &&
548556
(firstProto !== obj ||
549557
!builtInObjects.has(descriptor.value.name))) {
Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+11Lines changed: 11 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.