Skip to content

Commit 2664dac

Browse files
BridgeARTrott
authored andcommitted
util: validate formatWithOptions inspectOptions
This makes sure that the `inspectOptions` are validated. This could otherwise cause confusion. Fixes: #29726 PR-URL: #29824 Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 3aeae8d commit 2664dac

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/internal/util/inspect.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1542,11 +1542,6 @@ function reduceToSingleString(
15421542
return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`;
15431543
}
15441544

1545-
function format(...args) {
1546-
return formatWithOptions(undefined, ...args);
1547-
}
1548-
1549-
15501545
const firstErrorLine = (error) => error.message.split('\n')[0];
15511546
let CIRCULAR_ERROR_MESSAGE;
15521547
function tryStringify(arg) {
@@ -1569,7 +1564,19 @@ function tryStringify(arg) {
15691564
}
15701565
}
15711566

1567+
function format(...args) {
1568+
return formatWithOptionsInternal(undefined, ...args);
1569+
}
1570+
15721571
function formatWithOptions(inspectOptions, ...args) {
1572+
if (typeof inspectOptions !== 'object' || inspectOptions === null) {
1573+
throw new ERR_INVALID_ARG_TYPE(
1574+
'inspectOptions', 'object', inspectOptions);
1575+
}
1576+
return formatWithOptionsInternal(inspectOptions, ...args);
1577+
}
1578+
1579+
function formatWithOptionsInternal(inspectOptions, ...args) {
15731580
const first = args[0];
15741581
let a = 0;
15751582
let str = '';

test/parallel/test-util-format.js

+17
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,20 @@ assert.strictEqual(
408408
),
409409
'[ 1, [Object] ]'
410410
);
411+
412+
[
413+
undefined,
414+
null,
415+
false,
416+
5n,
417+
5,
418+
'test',
419+
Symbol()
420+
].forEach((invalidOptions) => {
421+
assert.throws(() => {
422+
util.formatWithOptions(invalidOptions, { a: true });
423+
}, {
424+
code: 'ERR_INVALID_ARG_TYPE',
425+
message: /"inspectOptions".+object/
426+
});
427+
});

0 commit comments

Comments
 (0)