Skip to content

Commit

Permalink
Fix malloc handling for remoteport and username
Browse files Browse the repository at this point in the history
  • Loading branch information
mkj committed Apr 3, 2024
1 parent 2754255 commit 0ae345e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/cli-runopts.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ void cli_getopts(int argc, char ** argv) {
const char* idle_timeout_arg = NULL;
const char *host_arg = NULL;
const char *proxycmd_arg = NULL;
const char *remoteport_arg = NULL;
const char *username_arg = NULL;
char c;

/* see printhelp() for options */
Expand Down Expand Up @@ -230,7 +232,7 @@ void cli_getopts(int argc, char ** argv) {
cli_opts.quiet = 1;
break;
case 'p': /* remoteport */
next = &cli_opts.remoteport;
next = &remoteport_arg;
break;
#if DROPBEAR_CLI_PUBKEY_AUTH
case 'i': /* an identityfile */
Expand Down Expand Up @@ -279,7 +281,7 @@ void cli_getopts(int argc, char ** argv) {
break;
#endif
case 'l':
next = &cli_opts.username;
next = &username_arg;
break;
case 'h':
printhelp();
Expand Down Expand Up @@ -408,13 +410,17 @@ void cli_getopts(int argc, char ** argv) {
#endif

/* Apply needed defaults if missing from command line or config file. */
if (cli_opts.remoteport == NULL) {
if (remoteport_arg) {
m_free(cli_opts.remoteport);
cli_opts.remoteport = m_strdup(remoteport_arg);
} else if (!cli_opts.remoteport) {
cli_opts.remoteport = m_strdup("22");
}

if (cli_opts.username) {
cli_opts.username = m_strdup(cli_opts.username);
} else {
if (username_arg) {
m_free(cli_opts.username);
cli_opts.username = m_strdup(username_arg);
} else if(!cli_opts.username) {
cli_opts.username = m_strdup(cli_opts.own_user);
}

Expand Down Expand Up @@ -998,7 +1004,7 @@ static void add_extendedopt(const char* origstr) {
}

if (match_extendedopt(&optstr, "Port") == DROPBEAR_SUCCESS) {
cli_opts.remoteport = optstr;
cli_opts.remoteport = m_strdup(optstr);
return;
}

Expand Down
7 changes: 3 additions & 4 deletions src/runopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,15 @@ void svr_getopts(int argc, char ** argv);
void loadhostkeys(void);

typedef struct cli_runopts {
/* All non-const strings are malloced */

char *progname;
/* malloced */
const char *progname;
char *remotehost;
int remotehostfixed;
/* malloced */
char *remoteport;

char *own_user;
const char *username;
char *username;

char *cmd;
int wantpty;
Expand Down

0 comments on commit 0ae345e

Please sign in to comment.