Skip to content

Commit a915f06

Browse files
authored
Set argv[argc] to NULL when calling main (#755)
* Set argv[argc] to NULL when calling main ISO C states that argv[argc] shall be a null pointer. The OpenSSH codebase does not appear to rely on this currently, but better to be safe in case something changes. * Check for malloc failure in sshd wmain
1 parent d7e886b commit a915f06

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

contrib/win32/win32compat/wmain_common.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ wmain(int argc, wchar_t **wargv) {
4343
char** argv = NULL;
4444
int i, r;
4545
_set_invalid_parameter_handler(invalid_parameter_handler);
46-
if (argc) {
47-
if ((argv = malloc(argc * sizeof(char*))) == NULL)
46+
if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL)
47+
fatal("out of memory");
48+
for (i = 0; i < argc; i++)
49+
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
4850
fatal("out of memory");
49-
for (i = 0; i < argc; i++)
50-
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
51-
fatal("out of memory");
52-
}
51+
argv[argc] = NULL;
5352

5453
if (getenv("SSH_AUTH_SOCK") == NULL)
5554
_putenv("SSH_AUTH_SOCK=\\\\.\\pipe\\openssh-ssh-agent");

contrib/win32/win32compat/wmain_sshd-session.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,13 @@ int sshd_session_main(int argc, wchar_t **wargv) {
5050
int i, r;
5151
_set_invalid_parameter_handler(invalid_parameter_handler);
5252

53-
if (argc) {
54-
if ((argv = malloc(argc * sizeof(char*))) == NULL) {
55-
printf("out of memory");
56-
exit(255);
57-
}
53+
if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL)
54+
fatal("out of memory");
5855

59-
for (i = 0; i < argc; i++)
60-
argv[i] = utf16_to_utf8(wargv[i]);
61-
}
56+
for (i = 0; i < argc; i++)
57+
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
58+
fatal("out of memory");
59+
argv[argc] = NULL;
6260

6361
w32posix_initialize();
6462

contrib/win32/win32compat/wmain_sshd.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,14 @@ int sshd_main(int argc, wchar_t **wargv) {
202202
int i, r;
203203
_set_invalid_parameter_handler(invalid_parameter_handler);
204204

205-
if (argc) {
206-
if ((argv = malloc(argc * sizeof(char*))) == NULL) {
207-
printf("out of memory");
208-
exit(255);
209-
}
205+
if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL)
206+
fatal("out of memory");
210207

211-
for (i = 0; i < argc; i++)
212-
argv[i] = utf16_to_utf8(wargv[i]);
213-
}
208+
for (i = 0; i < argc; i++)
209+
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
210+
fatal("out of memory");
211+
212+
argv[argc] = NULL;
214213

215214
w32posix_initialize();
216215

0 commit comments

Comments
 (0)