From 21a45e89c5839a6f25687359fb3c42a3123d33d8 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 10 Jan 2013 12:09:56 +0100 Subject: [PATCH] options: Fix a free crash bug if we parse unknown options. Thanks to Yong Chuan Koh, X-Force Research --- src/options.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/options.c b/src/options.c index c4589fe8..c43e41d4 100644 --- a/src/options.c +++ b/src/options.c @@ -953,12 +953,6 @@ int ssh_options_getopt(ssh_session session, int *argcptr, char **argv) { int saveoptind = optind; /* need to save 'em */ int saveopterr = opterr; - save = malloc(argc * sizeof(char *)); - if (save == NULL) { - ssh_set_error_oom(session); - return -1; - } - opterr = 0; /* shut up getopt */ while(cont && ((i = getopt(argc, argv, "c:i:Cl:p:vb:rd12")) != -1)) { switch(i) { @@ -996,8 +990,16 @@ int ssh_options_getopt(ssh_session session, int *argcptr, char **argv) { break; default: { + char **tmp; char opt[3]="- "; opt[1] = optopt; + tmp = realloc(save, (current + 1) * sizeof(char*)); + if (tmp == NULL) { + SAFE_FREE(save); + ssh_set_error_oom(session); + return -1; + } + save = tmp; save[current] = strdup(opt); if (save[current] == NULL) { SAFE_FREE(save);