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 6a16012

Browse filesBrowse files
ljharbaduh95
authored andcommitted
util: inspect: do not crash on an Error with a regex name
See #56570 PR-URL: #56574 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 9bd438a commit 6a16012
Copy full SHA for 6a16012

File tree

Expand file treeCollapse file tree

2 files changed

+31
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+31
-8
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
+14-3Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const {
8888
StringPrototypePadEnd,
8989
StringPrototypePadStart,
9090
StringPrototypeRepeat,
91+
StringPrototypeReplace,
9192
StringPrototypeReplaceAll,
9293
StringPrototypeSlice,
9394
StringPrototypeSplit,
@@ -733,6 +734,7 @@ function addPrototypeProperties(ctx, main, obj, recurseTimes, output) {
733734
} while (++depth !== 3);
734735
}
735736

737+
/** @type {(constructor: string, tag: string, fallback: string, size?: string) => string} */
736738
function getPrefix(constructor, tag, fallback, size = '') {
737739
if (constructor === null) {
738740
if (tag !== '' && fallback !== tag) {
@@ -1316,11 +1318,20 @@ function getStackFrames(ctx, err, stack) {
13161318
return frames;
13171319
}
13181320

1321+
/** @type {(stack: string, constructor: string | null, name: unknown, tag: string) => string} */
13191322
function improveStack(stack, constructor, name, tag) {
13201323
// A stack trace may contain arbitrary data. Only manipulate the output
13211324
// for "regular errors" (errors that "look normal") for now.
13221325
let len = name.length;
13231326

1327+
if (typeof name !== 'string') {
1328+
stack = StringPrototypeReplace(
1329+
stack,
1330+
`${name}`,
1331+
`${name} [${StringPrototypeSlice(getPrefix(constructor, tag, 'Error'), 0, -1)}]`,
1332+
);
1333+
}
1334+
13241335
if (constructor === null ||
13251336
(StringPrototypeEndsWith(name, 'Error') &&
13261337
StringPrototypeStartsWith(stack, name) &&
@@ -1353,8 +1364,8 @@ function removeDuplicateErrorKeys(ctx, keys, err, stack) {
13531364
if (!ctx.showHidden && keys.length !== 0) {
13541365
for (const name of ['name', 'message', 'stack']) {
13551366
const index = ArrayPrototypeIndexOf(keys, name);
1356-
// Only hide the property in case it's part of the original stack
1357-
if (index !== -1 && StringPrototypeIncludes(stack, err[name])) {
1367+
// Only hide the property if it's a string and if it's part of the original stack
1368+
if (index !== -1 && (typeof err[name] !== 'string' || StringPrototypeIncludes(stack, err[name]))) {
13581369
ArrayPrototypeSplice(keys, index, 1);
13591370
}
13601371
}
@@ -1414,7 +1425,7 @@ function safeGetCWD() {
14141425
}
14151426

14161427
function formatError(err, constructor, tag, ctx, keys) {
1417-
const name = err.name != null ? String(err.name) : 'Error';
1428+
const name = err.name != null ? err.name : 'Error';
14181429
let stack = getStackString(err);
14191430

14201431
removeDuplicateErrorKeys(ctx, keys, err, stack);
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+17-5Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,14 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
770770
// Note: Symbols are not supported by `Error#toString()` which is called by
771771
// accessing the `stack` property.
772772
[
773-
[404, '404: foo', '[404]'],
774-
[0, '0: foo', '[RangeError: foo]'],
775-
[0n, '0: foo', '[RangeError: foo]'],
773+
[404, '404 [RangeError]: foo', '[404]'],
774+
[0, '0 [RangeError]: foo', '[RangeError: foo]'],
775+
[0n, '0 [RangeError]: foo', '[RangeError: foo]'],
776776
[null, 'null: foo', '[RangeError: foo]'],
777777
[undefined, 'RangeError: foo', '[RangeError: foo]'],
778-
[false, 'false: foo', '[RangeError: foo]'],
778+
[false, 'false [RangeError]: foo', '[RangeError: foo]'],
779779
['', 'foo', '[RangeError: foo]'],
780-
[[1, 2, 3], '1,2,3: foo', '[1,2,3]'],
780+
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[1,2,3]'],
781781
].forEach(([value, outputStart, stack]) => {
782782
let err = new RangeError('foo');
783783
err.name = value;
@@ -3404,3 +3404,15 @@ assert.strictEqual(
34043404
'[Function: Symbol(f)]',
34053405
);
34063406
}
3407+
3408+
{
3409+
const error = new EvalError();
3410+
const re = /a/g;
3411+
error.name = re;
3412+
assert.strictEqual(error.name, re);
3413+
assert.strictEqual(
3414+
util.inspect(error),
3415+
`${re} [EvalError]
3416+
${error.stack.split('\n').slice(1).join('\n')}`,
3417+
);
3418+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.