Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: inspect: do not crash on Symbol function names #56572

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
keys = getKeys(value, ctx.showHidden);
braces = ['{', '}'];
if (typeof value === 'function') {
base = getFunctionBase(value, constructor, tag);
base = getFunctionBase(ctx, value, constructor, tag);
if (keys.length === 0 && protoProps === undefined)
return ctx.stylize(base, 'special');
} else if (constructor === 'Object') {
Expand Down Expand Up @@ -1223,7 +1223,7 @@ function getClassBase(value, constructor, tag) {
return `[${base}]`;
}

function getFunctionBase(value, constructor, tag) {
function getFunctionBase(ctx, value, constructor, tag) {
const stringified = FunctionPrototypeToString(value);
if (StringPrototypeStartsWith(stringified, 'class') && stringified[stringified.length - 1] === '}') {
const slice = StringPrototypeSlice(stringified, 5, -1);
Expand All @@ -1250,7 +1250,7 @@ function getFunctionBase(value, constructor, tag) {
if (value.name === '') {
base += ' (anonymous)';
} else {
base += `: ${value.name}`;
base += `: ${typeof value.name === 'string' ? value.name : formatValue(ctx, value.name)}`;
}
base += ']';
if (constructor !== type && constructor !== null) {
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3426,3 +3426,13 @@ assert.strictEqual(
Object.defineProperty(BuiltinPrototype, 'constructor', desc);
}
}

{
function f() {}
Object.defineProperty(f, 'name', { value: Symbol('f') });

assert.strictEqual(
util.inspect(f),
'[Function: Symbol(f)]',
);
}
Loading