Skip to content

Commit e6a9f11

Browse files
committed
validate arguments to mustCall(), default value
Updates expected to use modern default syntax and also validate the value and throw an error rather than silently overwriting invalid arguments. Would prevent issues such as the one fixed in the first commit on nodejs#9031
1 parent 6de9d13 commit e6a9f11

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

test/common.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,10 @@ function runCallChecks(exitCode) {
402402
}
403403

404404

405-
exports.mustCall = function(fn, expected) {
406-
if (typeof expected !== 'number') expected = 1;
405+
exports.mustCall = function(fn, expected = 1) {
406+
if (typeof expected !== 'number' || expected < 0) {
407+
throw new RangeError(`Invalid expected value: ${expected}`);
408+
}
407409

408410
var context = {
409411
expected: expected,

test/parallel/test-common.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@ var assert = require('assert');
55
common.globalCheck = false;
66
global.gc = 42; // Not a valid global unless --expose_gc is set.
77
assert.deepStrictEqual(common.leakedGlobals(), ['gc']);
8+
9+
10+
assert.throws(function() {
11+
common.mustCall(function() {},'foo')();
12+
}, /invalid expected value: foo/i);
13+
14+
assert.throws(function() {
15+
common.mustCall(function() {},/foo/)();
16+
}, /invalid expected value: \/foo\//i);

0 commit comments

Comments
 (0)