Skip to content

Commit 426aa0a

Browse files
Bryce SimondsMyles Borins
Bryce Simonds
authored and
Myles Borins
committed
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 9cccaa3 commit 426aa0a

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
@@ -3,7 +3,7 @@
33
#ifdef _WIN32
44
int wmain(int argc, wchar_t *wargv[]) {
55
// Convert argv to to UTF8
6-
char** argv = new char*[argc];
6+
char** argv = new char*[argc + 1];
77
for (int i = 0; i < argc; i++) {
88
// Compute the size of the required buffer
99
DWORD size = WideCharToMultiByte(CP_UTF8,
@@ -35,6 +35,7 @@ int wmain(int argc, wchar_t *wargv[]) {
3535
exit(1);
3636
}
3737
}
38+
argv[argc] = nullptr;
3839
// Now that conversion is done, we can finally start.
3940
return node::Start(argc, argv);
4041
}

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
@@ -59,6 +59,12 @@ child.exec(nodejs + ' --eval "require(\'./test/parallel/test-cli-eval.js\')"',
5959
assert.equal(status.code, 42);
6060
});
6161

62+
// Missing argument should not crash
63+
child.exec(nodejs + ' -e', common.mustCall(function(status, stdout, stderr) {
64+
assert.notStrictEqual(status, null);
65+
assert.strictEqual(status.code, 9);
66+
}));
67+
6268
// empty program should do nothing
6369
child.exec(nodejs + ' -e ""', function(status, stdout, stderr) {
6470
assert.equal(stdout, '');

0 commit comments

Comments
 (0)