From a5f91ab230c574d561780b6867d00f06fcc1e4de Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Thu, 30 Mar 2017 02:35:03 -0400 Subject: [PATCH] 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 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau --- src/node.cc | 6 ++++++ test/parallel/test-cli-syntax.js | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/node.cc b/src/node.cc index 853e0aa796ce4e..f1a3493bf1a252 100644 --- a/src/node.cc +++ b/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)); diff --git a/test/parallel/test-cli-syntax.js b/test/parallel/test-cli-syntax.js index 07dd3c4712e751..953e8dbcf60630 100644 --- a/test/parallel/test-cli-syntax.js +++ b/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); + }); +});