From 166bc2c4819dbbfaa881ffd42021cf044f716041 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sun, 16 Nov 2014 23:41:06 +0000 Subject: [PATCH] Cleanup client memory on error. --- client/client_shared.c | 65 ++++++++++++++++++++++++++++++++++++++++++ client/client_shared.h | 1 + client/pub_client.c | 1 + client/sub_client.c | 1 + 4 files changed, 68 insertions(+) diff --git a/client/client_shared.c b/client/client_shared.c index 4b228ae1..d2484dcd 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -44,6 +44,51 @@ void init_config(struct mosq_config *cfg) cfg->eol = true; } +void client_config_cleanup(struct mosq_config *cfg) +{ + int i; + if(cfg->id) free(cfg->id); + if(cfg->id_prefix) free(cfg->id_prefix); + if(cfg->host) free(cfg->host); + if(cfg->file_input) free(cfg->file_input); + if(cfg->message) free(cfg->message); + if(cfg->topic) free(cfg->topic); + if(cfg->bind_address) free(cfg->bind_address); + if(cfg->username) free(cfg->username); + if(cfg->password) free(cfg->password); + if(cfg->will_topic) free(cfg->will_topic); + if(cfg->will_payload) free(cfg->will_payload); +#ifdef WITH_TLS + if(cfg->cafile) free(cfg->cafile); + if(cfg->capath) free(cfg->capath); + if(cfg->certfile) free(cfg->certfile); + if(cfg->keyfile) free(cfg->keyfile); + if(cfg->ciphers) free(cfg->ciphers); + if(cfg->tls_version) free(cfg->tls_version); +# ifdef WITH_TLS_PSK + if(cfg->psk) free(cfg->psk); + if(cfg->psk_identity) free(cfg->psk_identity); +# endif +#endif + if(cfg->topics){ + for(i=0; itopic_count; i++){ + free(cfg->topics[i]); + } + free(cfg->topics); + } + if(cfg->filter_outs){ + for(i=0; ifilter_out_count; i++){ + free(cfg->filter_outs[i]); + } + free(cfg->filter_outs); + } +#ifdef WITH_SOCKS + if(cfg->socks5_host) free(cfg->socks5_host); + if(cfg->socks5_username) free(cfg->socks5_username); + if(cfg->socks5_password) free(cfg->socks5_password); +#endif +} + int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *argv[]) { int rc; @@ -755,6 +800,13 @@ static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url) return 1; } + // socks5h://username:password@host:1883 + // socks5h://username:password@host + // socks5h://username@host:1883 + // socks5h://username@host + // socks5h://host:1883 + // socks5h://host + start = 0; for(i=0; i start){ len = i-start; if(host){ + /* Have already seen a @ , so this must be of form + * socks5h://username[:password]@host:port */ port = malloc(len + 1); memcpy(port, &(str[start]), len); port[len] = '\0'; }else if(username_or_host){ + /* Haven't seen a @ before, so must be of form + * socks5h://host:port or + * socks5h://username:password@host[:port] */ if(have_auth){ host = malloc(len + 1); memcpy(host, &(str[start]), len); diff --git a/client/client_shared.h b/client/client_shared.h index aaa9efad..2f828677 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -89,6 +89,7 @@ struct mosq_config { }; int client_config_load(struct mosq_config *config, int pub_or_sub, int argc, char *argv[]); +void client_config_cleanup(struct mosq_config *cfg); int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg); int client_id_generate(struct mosq_config *cfg, const char *id_base); int client_connect(struct mosquitto *mosq, struct mosq_config *cfg); diff --git a/client/pub_client.c b/client/pub_client.c index b20cb780..dbdd3469 100644 --- a/client/pub_client.c +++ b/client/pub_client.c @@ -289,6 +289,7 @@ int main(int argc, char *argv[]) rc = client_config_load(&cfg, CLIENT_PUB, argc, argv); if(rc){ + client_config_cleanup(&cfg); if(rc == 2){ /* --help */ print_usage(); diff --git a/client/sub_client.c b/client/sub_client.c index 1f3fc1a6..e7252e64 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -212,6 +212,7 @@ int main(int argc, char *argv[]) rc = client_config_load(&cfg, CLIENT_SUB, argc, argv); if(rc){ + client_config_cleanup(&cfg); if(rc == 2){ /* --help */ print_usage();