Skip to content

Commit cd38401

Browse files
Bryce Simondsevanlucas
Bryce Simonds
authored andcommitted
src: fix Windows segfault with --eval
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 <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent cd7c29e commit cd38401

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/node_main.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int wmain(int argc, wchar_t *wargv[]) {
1111
}
1212

1313
// Convert argv to to UTF8
14-
char** argv = new char*[argc];
14+
char** argv = new char*[argc + 1];
1515
for (int i = 0; i < argc; i++) {
1616
// Compute the size of the required buffer
1717
DWORD size = WideCharToMultiByte(CP_UTF8,
@@ -43,6 +43,7 @@ int wmain(int argc, wchar_t *wargv[]) {
4343
exit(1);
4444
}
4545
}
46+
argv[argc] = nullptr;
4647
// Now that conversion is done, we can finally start.
4748
return node::Start(argc, argv);
4849
}

test/parallel/test-cli-eval.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ child.exec(nodejs + ' --eval "console.error(42)"',
4040
assert.equal(stderr, '');
4141
});
4242

43-
child.exec(cmd + "'[]'",
43+
child.exec(cmd + "'[]'", common.mustCall(
4444
function(err, stdout, stderr) {
4545
assert.equal(stdout, '[]\n');
4646
assert.equal(stderr, '');
47-
});
47+
}));
4848
});
4949

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

69+
// Missing argument should not crash
70+
child.exec(nodejs + ' -e', common.mustCall(function(status, stdout, stderr) {
71+
assert.notStrictEqual(status, null);
72+
assert.strictEqual(status.code, 9);
73+
}));
74+
6975
// empty program should do nothing
7076
child.exec(nodejs + ' -e ""', function(status, stdout, stderr) {
7177
assert.equal(stdout, '');

0 commit comments

Comments
 (0)