Browse Source

src: throw when -c and -e are used simultaneously

The -c flag ("check script syntax") and -e flag ("evaluate given code")
have contradictory meanings. Make them mutually exclusive by throwing
when both of them are provided.

Fixes: https://github.com/nodejs/node/issues/11680
PR-URL: https://github.com/nodejs/node/pull/11689
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
v6
Teddy Katz 8 years ago
parent
commit
a5f91ab230
No known key found for this signature in database GPG Key ID: B79EC7ABC0AA53A0
  1. 6
      src/node.cc
  2. 17
      test/parallel/test-cli-syntax.js

6
src/node.cc

@ -3816,6 +3816,12 @@ static void ParseArgs(int* argc,
}
#endif
if (eval_string != nullptr && syntax_check_only) {
fprintf(stderr,
"%s: either --check or --eval can be used, not both\n", argv[0]);
exit(9);
}
// Copy remaining arguments.
const unsigned int args_left = nargs - index;
memcpy(new_argv + new_argc, argv + index, args_left * sizeof(*argv));

17
test/parallel/test-cli-syntax.js

@ -99,7 +99,7 @@ syntaxArgs.forEach(function(args) {
assert.strictEqual(c.status, 0, 'code === ' + c.status);
});
// should should throw if code piped from stdin with --check has bad syntax
// should throw if code piped from stdin with --check has bad syntax
// loop each possible option, `-c` or `--check`
syntaxArgs.forEach(function(args) {
const stdin = 'var foo bar;';
@ -117,3 +117,18 @@ syntaxArgs.forEach(function(args) {
assert.strictEqual(c.status, 1, 'code === ' + c.status);
});
// should throw if -c and -e flags are both passed
['-c', '--check'].forEach(function(checkFlag) {
['-e', '--eval'].forEach(function(evalFlag) {
const args = [checkFlag, evalFlag, 'foo'];
const c = spawnSync(node, args, {encoding: 'utf8'});
assert.strictEqual(
c.stderr,
`${node}: either --check or --eval can be used, not both\n`
);
assert.strictEqual(c.status, 9, 'code === ' + c.status);
});
});

Loading…
Cancel
Save