From b726e8a3edc44616aa592593bad51533d843fb9b Mon Sep 17 00:00:00 2001 From: Mike Gilbert Date: Wed, 9 Oct 2024 16:24:26 -0400 Subject: [PATCH] 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. --- contrib/win32/win32compat/wmain_common.c | 11 +++++------ contrib/win32/win32compat/wmain_sshd-session.c | 15 +++++++-------- contrib/win32/win32compat/wmain_sshd.c | 15 +++++++-------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/contrib/win32/win32compat/wmain_common.c b/contrib/win32/win32compat/wmain_common.c index 9f96ca7ce..9a5784bb6 100644 --- a/contrib/win32/win32compat/wmain_common.c +++ b/contrib/win32/win32compat/wmain_common.c @@ -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"); diff --git a/contrib/win32/win32compat/wmain_sshd-session.c b/contrib/win32/win32compat/wmain_sshd-session.c index 140030621..804bb50f5 100644 --- a/contrib/win32/win32compat/wmain_sshd-session.c +++ b/contrib/win32/win32compat/wmain_sshd-session.c @@ -50,16 +50,15 @@ 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); - } - - for (i = 0; i < argc; i++) - argv[i] = utf16_to_utf8(wargv[i]); + if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL) { + printf("out of memory"); + exit(255); } + for (i = 0; i < argc; i++) + argv[i] = utf16_to_utf8(wargv[i]); + argv[argc] = NULL; + w32posix_initialize(); r = main(argc, argv); diff --git a/contrib/win32/win32compat/wmain_sshd.c b/contrib/win32/win32compat/wmain_sshd.c index 1b4bc78c6..41c9ea572 100644 --- a/contrib/win32/win32compat/wmain_sshd.c +++ b/contrib/win32/win32compat/wmain_sshd.c @@ -202,16 +202,15 @@ 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); - } - - for (i = 0; i < argc; i++) - argv[i] = utf16_to_utf8(wargv[i]); + if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL) { + printf("out of memory"); + exit(255); } + for (i = 0; i < argc; i++) + argv[i] = utf16_to_utf8(wargv[i]); + argv[argc] = NULL; + w32posix_initialize(); r = main(argc, argv);