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
This commit is contained in:
Mike Gilbert 2024-10-14 17:43:35 -04:00 committed by GitHub
parent d7e886b9f6
commit a915f06c78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 22 deletions

View File

@ -43,13 +43,12 @@ wmain(int argc, wchar_t **wargv) {
char** argv = NULL;
int i, r;
_set_invalid_parameter_handler(invalid_parameter_handler);
if (argc) {
if ((argv = malloc(argc * sizeof(char*))) == NULL)
if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL)
fatal("out of memory");
for (i = 0; i < argc; i++)
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
fatal("out of memory");
for (i = 0; i < argc; i++)
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
fatal("out of memory");
}
argv[argc] = NULL;
if (getenv("SSH_AUTH_SOCK") == NULL)
_putenv("SSH_AUTH_SOCK=\\\\.\\pipe\\openssh-ssh-agent");

View File

@ -50,15 +50,13 @@ int sshd_session_main(int argc, wchar_t **wargv) {
int i, r;
_set_invalid_parameter_handler(invalid_parameter_handler);
if (argc) {
if ((argv = malloc(argc * sizeof(char*))) == NULL) {
printf("out of memory");
exit(255);
}
if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL)
fatal("out of memory");
for (i = 0; i < argc; i++)
argv[i] = utf16_to_utf8(wargv[i]);
}
for (i = 0; i < argc; i++)
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
fatal("out of memory");
argv[argc] = NULL;
w32posix_initialize();

View File

@ -202,15 +202,14 @@ int sshd_main(int argc, wchar_t **wargv) {
int i, r;
_set_invalid_parameter_handler(invalid_parameter_handler);
if (argc) {
if ((argv = malloc(argc * sizeof(char*))) == NULL) {
printf("out of memory");
exit(255);
}
if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL)
fatal("out of memory");
for (i = 0; i < argc; i++)
argv[i] = utf16_to_utf8(wargv[i]);
}
for (i = 0; i < argc; i++)
if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL)
fatal("out of memory");
argv[argc] = NULL;
w32posix_initialize();