1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-29 01:03:57 +03:00

options: Properly handle unknown options with arguments

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Jakub Jelen
2020-04-29 14:18:59 +02:00
committed by Andreas Schneider
parent b90131dfe6
commit 2e7ca3e8a6

View File

@@ -1217,6 +1217,11 @@ int ssh_options_getopt(ssh_session session, int *argcptr, char **argv)
int saveopterr = opterr; int saveopterr = opterr;
int opt; int opt;
/* Nothing to do here */
if (argc <= 1) {
return SSH_OK;
}
opterr = 0; /* shut up getopt */ opterr = 0; /* shut up getopt */
while((opt = getopt(argc, argv, "c:i:Cl:p:vb:rd12")) != -1) { while((opt = getopt(argc, argv, "c:i:Cl:p:vb:rd12")) != -1) {
switch(opt) { switch(opt) {
@@ -1266,8 +1271,19 @@ int ssh_options_getopt(ssh_session session, int *argcptr, char **argv)
return -1; return -1;
} }
current++; current++;
if (optarg) { /* We can not use optarg here as getopt does not set it for
save[current++] = argv[optind + 1]; * unknown options. We need to manually extract following
* option and skip it manually from further processing */
if (optind < argc && argv[optind][0] != '-') {
tmp = realloc(save, (current + 1) * sizeof(char*));
if (tmp == NULL) {
SAFE_FREE(save);
ssh_set_error_oom(session);
return -1;
}
save = tmp;
save[current++] = argv[optind];
optind++;
} }
} }
} /* switch */ } /* switch */