Skip to content

Commit 3fd4288

Browse files
author
huangweihao
committed
util: fix inspect crash when getter returns a function
When a property getter returns a function, formatProperty incorrectly routes the function value to formatPrimitive, which does not handle functions and falls through to Symbol.prototype.toString(), causing a TypeError. Fix by treating getter-returned functions the same as getter-returned objects in formatProperty, routing them through formatValue which properly formats functions. Before (broken): getFunction: [Getter: <Inspection threw (TypeError: ...)>] After (fixed): getFunction: [Getter] [Function: fn] Fixes: #64838
1 parent c8e2a82 commit 3fd4288

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/internal/util/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
25762576
const tmp = FunctionPrototypeCall(desc.get, original);
25772577
if (tmp === null) {
25782578
str = `${s(`[${label}:`, sp)} ${s('null', 'null')}${s(']', sp)}`;
2579-
} else if (typeof tmp === 'object') {
2579+
} else if (typeof tmp === 'object' || typeof tmp === 'function') {
25802580
str = `${s(`[${label}]`, sp)} ${formatValue(ctx, tmp, recurseTimes)}`;
25812581
} else {
25822582
const primitive = formatPrimitive(s, tmp, ctx);

test/parallel/test-util-inspect.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,26 @@ assert.strictEqual(
25962596
"'foobar', { x: 1 } },\n inc: [Getter: NaN]\n}");
25972597
}
25982598

2599+
// Property getter returning a function.
2600+
{
2601+
const getterFn = {
2602+
get getString() { return 'foo'; },
2603+
string: 'foo',
2604+
get getObject() { return { nested: true }; },
2605+
object: { nested: true },
2606+
get getFunction() { return function fn() {}; },
2607+
function: function fn() {},
2608+
};
2609+
assert.strictEqual(
2610+
inspect(getterFn, { getters: true }),
2611+
"{ getString: [Getter: 'foo'],\n" +
2612+
" string: 'foo',\n" +
2613+
' getObject: [Getter] { nested: true },\n' +
2614+
" object: { nested: true },\n" +
2615+
' getFunction: [Getter] [Function: fn],\n' +
2616+
' function: [Function: fn] }');
2617+
}
2618+
25992619
// Property getter throwing an error.
26002620
{
26012621
const error = new Error('Oops');

0 commit comments

Comments
 (0)