Skip to content

Commit

Permalink
src: fix Windows segfault with --eval
Browse files Browse the repository at this point in the history
When specifing a parameter that requries an additional argument on the
command line, node would segfault.  This appears to be specific to
Windows, adjusted command line argument parsing to hold a nullptr
terminal.

Adding unit test for crash on missing arguments.

PR-URL: #6938
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
Bryce Simonds authored and Myles Borins committed Jul 14, 2016
1 parent 9cccaa3 commit 426aa0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/node_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifdef _WIN32
int wmain(int argc, wchar_t *wargv[]) {
// Convert argv to to UTF8
char** argv = new char*[argc];
char** argv = new char*[argc + 1];
for (int i = 0; i < argc; i++) {
// Compute the size of the required buffer
DWORD size = WideCharToMultiByte(CP_UTF8,
Expand Down Expand Up @@ -35,6 +35,7 @@ int wmain(int argc, wchar_t *wargv[]) {
exit(1);
}
}
argv[argc] = nullptr;
// Now that conversion is done, we can finally start.
return node::Start(argc, argv);
}
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-cli-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ child.exec(nodejs + ' --eval "console.error(42)"',
assert.equal(stderr, '');
});

child.exec(cmd + "'[]'",
child.exec(cmd + "'[]'", common.mustCall(
function(err, stdout, stderr) {
assert.equal(stdout, '[]\n');
assert.equal(stderr, '');
});
}));
});

// assert that module loading works
Expand All @@ -59,6 +59,12 @@ child.exec(nodejs + ' --eval "require(\'./test/parallel/test-cli-eval.js\')"',
assert.equal(status.code, 42);
});

// Missing argument should not crash
child.exec(nodejs + ' -e', common.mustCall(function(status, stdout, stderr) {
assert.notStrictEqual(status, null);
assert.strictEqual(status.code, 9);
}));

// empty program should do nothing
child.exec(nodejs + ' -e ""', function(status, stdout, stderr) {
assert.equal(stdout, '');
Expand Down

0 comments on commit 426aa0a

Please sign in to comment.