diff --git a/lib/memory_mosq.c b/lib/memory_mosq.c index 4d8217db..6c9990a2 100644 --- a/lib/memory_mosq.c +++ b/lib/memory_mosq.c @@ -42,9 +42,11 @@ void *mosquitto__calloc(size_t nmemb, size_t size) void *mem = calloc(nmemb, size); #ifdef REAL_WITH_MEMORY_TRACKING - memcount += malloc_usable_size(mem); - if(memcount > max_memcount){ - max_memcount = memcount; + if(mem){ + memcount += malloc_usable_size(mem); + if(memcount > max_memcount){ + max_memcount = memcount; + } } #endif @@ -67,9 +69,11 @@ void *mosquitto__malloc(size_t size) void *mem = malloc(size); #ifdef REAL_WITH_MEMORY_TRACKING - memcount += malloc_usable_size(mem); - if(memcount > max_memcount){ - max_memcount = memcount; + if(mem){ + memcount += malloc_usable_size(mem); + if(memcount > max_memcount){ + max_memcount = memcount; + } } #endif @@ -99,9 +103,11 @@ void *mosquitto__realloc(void *ptr, size_t size) mem = realloc(ptr, size); #ifdef REAL_WITH_MEMORY_TRACKING - memcount += malloc_usable_size(mem); - if(memcount > max_memcount){ - max_memcount = memcount; + if(mem){ + memcount += malloc_usable_size(mem); + if(memcount > max_memcount){ + max_memcount = memcount; + } } #endif @@ -113,9 +119,11 @@ char *mosquitto__strdup(const char *s) char *str = strdup(s); #ifdef REAL_WITH_MEMORY_TRACKING - memcount += malloc_usable_size(str); - if(memcount > max_memcount){ - max_memcount = memcount; + if(str){ + memcount += malloc_usable_size(str); + if(memcount > max_memcount){ + max_memcount = memcount; + } } #endif diff --git a/lib/mosquitto.c b/lib/mosquitto.c index 5f005725..b8c7e3d2 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -233,14 +233,11 @@ int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, cons { if(!mosq) return MOSQ_ERR_INVAL; - if(mosq->username){ - mosquitto__free(mosq->username); - mosq->username = NULL; - } - if(mosq->password){ - mosquitto__free(mosq->password); - mosq->password = NULL; - } + mosquitto__free(mosq->username); + mosq->username = NULL; + + mosquitto__free(mosq->password); + mosq->password = NULL; if(username){ mosq->username = mosquitto__strdup(username); @@ -319,30 +316,23 @@ void mosquitto__destroy(struct mosquitto *mosq) mosquitto__free(mosq->tls_psk_identity); #endif - if(mosq->address){ - mosquitto__free(mosq->address); - mosq->address = NULL; - } - if(mosq->id){ - mosquitto__free(mosq->id); - mosq->id = NULL; - } - if(mosq->username){ - mosquitto__free(mosq->username); - mosq->username = NULL; - } - if(mosq->password){ - mosquitto__free(mosq->password); - mosq->password = NULL; - } - if(mosq->host){ - mosquitto__free(mosq->host); - mosq->host = NULL; - } - if(mosq->bind_address){ - mosquitto__free(mosq->bind_address); - mosq->bind_address = NULL; - } + mosquitto__free(mosq->address); + mosq->address = NULL; + + mosquitto__free(mosq->id); + mosq->id = NULL; + + mosquitto__free(mosq->username); + mosq->username = NULL; + + mosquitto__free(mosq->password); + mosq->password = NULL; + + mosquitto__free(mosq->host); + mosq->host = NULL; + + mosquitto__free(mosq->bind_address); + mosq->bind_address = NULL; /* Out packet cleanup */ if(mosq->out_packet && !mosq->current_out_packet){ @@ -636,6 +626,8 @@ int mosquitto_tls_set(struct mosquitto *mosq, const char *cafile, const char *ca if(!mosq || (!cafile && !capath) || (certfile && !keyfile) || (!certfile && keyfile)) return MOSQ_ERR_INVAL; + mosquitto__free(mosq->tls_cafile); + mosq->tls_cafile = NULL; if(cafile){ fptr = mosquitto__fopen(cafile, "rt"); if(fptr){ @@ -648,71 +640,58 @@ int mosquitto_tls_set(struct mosquitto *mosq, const char *cafile, const char *ca if(!mosq->tls_cafile){ return MOSQ_ERR_NOMEM; } - }else if(mosq->tls_cafile){ - mosquitto__free(mosq->tls_cafile); - mosq->tls_cafile = NULL; } + mosquitto__free(mosq->tls_capath); + mosq->tls_capath = NULL; if(capath){ mosq->tls_capath = mosquitto__strdup(capath); if(!mosq->tls_capath){ return MOSQ_ERR_NOMEM; } - }else if(mosq->tls_capath){ - mosquitto__free(mosq->tls_capath); - mosq->tls_capath = NULL; } + mosquitto__free(mosq->tls_certfile); + mosq->tls_certfile = NULL; if(certfile){ fptr = mosquitto__fopen(certfile, "rt"); if(fptr){ fclose(fptr); }else{ - if(mosq->tls_cafile){ - mosquitto__free(mosq->tls_cafile); - mosq->tls_cafile = NULL; - } - if(mosq->tls_capath){ - mosquitto__free(mosq->tls_capath); - mosq->tls_capath = NULL; - } + mosquitto__free(mosq->tls_cafile); + mosq->tls_cafile = NULL; + + mosquitto__free(mosq->tls_capath); + mosq->tls_capath = NULL; return MOSQ_ERR_INVAL; } mosq->tls_certfile = mosquitto__strdup(certfile); if(!mosq->tls_certfile){ return MOSQ_ERR_NOMEM; } - }else{ - mosquitto__free(mosq->tls_certfile); - mosq->tls_certfile = NULL; } + mosquitto__free(mosq->tls_keyfile); + mosq->tls_keyfile = NULL; if(keyfile){ fptr = mosquitto__fopen(keyfile, "rt"); if(fptr){ fclose(fptr); }else{ - if(mosq->tls_cafile){ - mosquitto__free(mosq->tls_cafile); - mosq->tls_cafile = NULL; - } - if(mosq->tls_capath){ - mosquitto__free(mosq->tls_capath); - mosq->tls_capath = NULL; - } - if(mosq->tls_certfile){ - mosquitto__free(mosq->tls_certfile); - mosq->tls_certfile = NULL; - } + mosquitto__free(mosq->tls_cafile); + mosq->tls_cafile = NULL; + + mosquitto__free(mosq->tls_capath); + mosq->tls_capath = NULL; + + mosquitto__free(mosq->tls_certfile); + mosq->tls_certfile = NULL; return MOSQ_ERR_INVAL; } mosq->tls_keyfile = mosquitto__strdup(keyfile); if(!mosq->tls_keyfile){ return MOSQ_ERR_NOMEM; } - }else{ - mosquitto__free(mosq->tls_keyfile); - mosq->tls_keyfile = NULL; } mosq->tls_pw_callback = pw_callback; diff --git a/lib/socks_mosq.c b/lib/socks_mosq.c index b9549a1a..0158db5a 100644 --- a/lib/socks_mosq.c +++ b/lib/socks_mosq.c @@ -49,9 +49,8 @@ int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, con if(!host || strlen(host) > 256) return MOSQ_ERR_INVAL; if(port < 1 || port > 65535) return MOSQ_ERR_INVAL; - if(mosq->socks5_host){ - mosquitto__free(mosq->socks5_host); - } + mosquitto__free(mosq->socks5_host); + mosq->socks5_host = NULL; mosq->socks5_host = mosquitto__strdup(host); if(!mosq->socks5_host){ @@ -60,12 +59,11 @@ int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, con mosq->socks5_port = port; - if(mosq->socks5_username){ - mosquitto__free(mosq->socks5_username); - } - if(mosq->socks5_password){ - mosquitto__free(mosq->socks5_password); - } + mosquitto__free(mosq->socks5_username); + mosq->socks5_username = NULL; + + mosquitto__free(mosq->socks5_password); + mosq->socks5_password = NULL; if(username){ mosq->socks5_username = mosquitto__strdup(username); @@ -75,10 +73,8 @@ int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, con if(password){ mosq->socks5_password = mosquitto__strdup(password); - if(!mosq->socks5_password){ - mosquitto__free(mosq->socks5_username); - return MOSQ_ERR_NOMEM; - } + mosquitto__free(mosq->socks5_username); + return MOSQ_ERR_NOMEM; } } diff --git a/lib/will_mosq.c b/lib/will_mosq.c index 86624c09..728ca299 100644 --- a/lib/will_mosq.c +++ b/lib/will_mosq.c @@ -39,16 +39,9 @@ int will__set(struct mosquitto *mosq, const char *topic, int payloadlen, const v if(mosquitto_pub_topic_check(topic)) return MOSQ_ERR_INVAL; if(mosq->will){ - if(mosq->will->topic){ - mosquitto__free(mosq->will->topic); - mosq->will->topic = NULL; - } - if(mosq->will->payload){ - mosquitto__free(mosq->will->payload); - mosq->will->payload = NULL; - } + mosquitto__free(mosq->will->topic); + mosquitto__free(mosq->will->payload); mosquitto__free(mosq->will); - mosq->will = NULL; } mosq->will = mosquitto__calloc(1, sizeof(struct mosquitto_message)); @@ -81,9 +74,10 @@ cleanup: if(mosq->will){ mosquitto__free(mosq->will->topic); mosquitto__free(mosq->will->payload); + + mosquitto__free(mosq->will); + mosq->will = NULL; } - mosquitto__free(mosq->will); - mosq->will = NULL; return rc; } @@ -92,14 +86,12 @@ int will__clear(struct mosquitto *mosq) { if(!mosq->will) return MOSQ_ERR_SUCCESS; - if(mosq->will->topic){ - mosquitto__free(mosq->will->topic); - mosq->will->topic = NULL; - } - if(mosq->will->payload){ - mosquitto__free(mosq->will->payload); - mosq->will->payload = NULL; - } + mosquitto__free(mosq->will->topic); + mosq->will->topic = NULL; + + mosquitto__free(mosq->will->payload); + mosq->will->payload = NULL; + mosquitto__free(mosq->will); mosq->will = NULL; diff --git a/src/conf.c b/src/conf.c index afe403b7..2540f97b 100644 --- a/src/conf.c +++ b/src/conf.c @@ -124,10 +124,9 @@ static void config__init_reload(struct mosquitto__config *config) fclose(config->log_fptr); config->log_fptr = NULL; } - if(config->log_file){ - mosquitto__free(config->log_file); - config->log_file = NULL; - } + mosquitto__free(config->log_file); + config->log_file = NULL; + #if defined(WIN32) || defined(__CYGWIN__) if(service_handle){ /* This is running as a Windows service. Default to no logging. Using @@ -163,10 +162,9 @@ static void config__init_reload(struct mosquitto__config *config) if(config->auth_plugins){ for(i=0; iauth_plugin_count; i++){ plug = &config->auth_plugins[i]; - if(plug->path){ - mosquitto__free(plug->path); - plug->path = NULL; - } + mosquitto__free(plug->path); + plug->path = NULL; + if(plug->options){ for(j=0; joption_count; j++){ mosquitto__free(plug->options[j].key); @@ -302,10 +300,8 @@ void config__cleanup(struct mosquitto__config *config) if(config->auth_plugins){ for(i=0; iauth_plugin_count; i++){ plug = &config->auth_plugins[i]; - if(plug->path){ - mosquitto__free(plug->path); - plug->path = NULL; - } + mosquitto__free(plug->path); + plug->path = NULL; if(plug->options){ for(j=0; joption_count; j++){ mosquitto__free(plug->options[j].key); @@ -509,9 +505,7 @@ int config__read(struct mosquitto__config *config, bool reload) config->persistence_file = mosquitto__strdup("mosquitto.db"); if(!config->persistence_file) return MOSQ_ERR_NOMEM; } - if(config->persistence_filepath){ - mosquitto__free(config->persistence_filepath); - } + mosquitto__free(config->persistence_filepath); if(config->persistence_location && strlen(config->persistence_location)){ len = strlen(config->persistence_location) + strlen(config->persistence_file) + 1; config->persistence_filepath = mosquitto__malloc(len); @@ -606,10 +600,8 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, const if(token){ if(!strcmp(token, "acl_file")){ if(reload){ - if(config->acl_file){ - mosquitto__free(config->acl_file); - config->acl_file = NULL; - } + mosquitto__free(config->acl_file); + config->acl_file = NULL; } if(conf__parse_string(&token, "acl_file", &config->acl_file, saveptr)) return MOSQ_ERR_INVAL; }else if(!strcmp(token, "address") || !strcmp(token, "addresses")){ @@ -1059,10 +1051,8 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, const #endif }else if(!strcmp(token, "clientid_prefixes")){ if(reload){ - if(config->clientid_prefixes){ - mosquitto__free(config->clientid_prefixes); - config->clientid_prefixes = NULL; - } + mosquitto__free(config->clientid_prefixes); + config->clientid_prefixes = NULL; } if(conf__parse_string(&token, "clientid_prefixes", &config->clientid_prefixes, saveptr)) return MOSQ_ERR_INVAL; }else if(!strcmp(token, "connection")){ @@ -1535,10 +1525,8 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, const #endif }else if(!strcmp(token, "password_file")){ if(reload){ - if(config->password_file){ - mosquitto__free(config->password_file); - config->password_file = NULL; - } + mosquitto__free(config->password_file); + config->password_file = NULL; } if(conf__parse_string(&token, "password_file", &config->password_file, saveptr)) return MOSQ_ERR_INVAL; }else if(!strcmp(token, "persistence") || !strcmp(token, "retained_persistence")){ @@ -1620,10 +1608,8 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, const }else if(!strcmp(token, "psk_file")){ #ifdef REAL_WITH_TLS_PSK if(reload){ - if(config->psk_file){ - mosquitto__free(config->psk_file); - config->psk_file = NULL; - } + mosquitto__free(config->psk_file); + config->psk_file = NULL; } if(conf__parse_string(&token, "psk_file", &config->psk_file, saveptr)) return MOSQ_ERR_INVAL; #else diff --git a/src/context.c b/src/context.c index bfd26d69..872b8548 100644 --- a/src/context.c +++ b/src/context.c @@ -98,14 +98,12 @@ void context__cleanup(struct mosquitto_db *db, struct mosquitto *context, bool d if(!context) return; - if(context->username){ - mosquitto__free(context->username); - context->username = NULL; - } - if(context->password){ - mosquitto__free(context->password); - context->password = NULL; - } + mosquitto__free(context->username); + context->username = NULL; + + mosquitto__free(context->password); + context->password = NULL; + #ifdef WITH_BRIDGE if(context->bridge){ for(i=0; ibridge_count; i++){ @@ -137,10 +135,9 @@ void context__cleanup(struct mosquitto_db *db, struct mosquitto *context, bool d sub__clean_session(db, context); db__messages_delete(db, context); } - if(context->address){ - mosquitto__free(context->address); - context->address = NULL; - } + + mosquitto__free(context->address); + context->address = NULL; if(context->id){ assert(db); /* db can only be NULL here if the client hasn't sent a diff --git a/src/database.c b/src/database.c index 78dbde54..009d2ee1 100644 --- a/src/database.c +++ b/src/database.c @@ -598,9 +598,7 @@ int db__message_store(struct mosquitto_db *db, const char *source, uint16_t sour return MOSQ_ERR_SUCCESS; error: - if(topic){ - mosquitto__free(topic); - } + mosquitto__free(topic); if(temp){ mosquitto__free(temp->source_id); mosquitto__free(temp->topic); diff --git a/src/mosquitto.c b/src/mosquitto.c index 67187e3d..17d49d1a 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -386,9 +386,7 @@ int main(int argc, char *argv[]) if(int_db.config->listeners[i].ws_context){ libwebsocket_context_destroy(int_db.config->listeners[i].ws_context); } - if(int_db.config->listeners[i].ws_protocol){ - mosquitto__free(int_db.config->listeners[i].ws_protocol); - } + mosquitto__free(int_db.config->listeners[i].ws_protocol); } #endif @@ -410,9 +408,7 @@ int main(int argc, char *argv[]) context__cleanup(&int_db, int_db.bridges[i], true); } } - if(int_db.bridges){ - mosquitto__free(int_db.bridges); - } + mosquitto__free(int_db.bridges); #endif context__free_disused(&int_db); diff --git a/src/security_default.c b/src/security_default.c index 82f29f3f..20a4c7bc 100644 --- a/src/security_default.c +++ b/src/security_default.c @@ -439,9 +439,7 @@ static void free__acl(struct mosquitto__acl *acl) if(acl->next){ free__acl(acl->next); } - if(acl->topic){ - mosquitto__free(acl->topic); - } + mosquitto__free(acl->topic); mosquitto__free(acl); } @@ -467,9 +465,7 @@ static int acl__cleanup(struct mosquitto_db *db, bool reload) user_tail = db->acl_list->next; free__acl(db->acl_list->acl); - if(db->acl_list->username){ - mosquitto__free(db->acl_list->username); - } + mosquitto__free(db->acl_list->username); mosquitto__free(db->acl_list); db->acl_list = user_tail;