Remove more unnecessary "if(x) mosquitto__free(x)" checks.

pull/139/head
Roger A. Light 10 years ago
parent 436d3fac19
commit b4fbe904d4

@ -42,9 +42,11 @@ void *mosquitto__calloc(size_t nmemb, size_t size)
void *mem = calloc(nmemb, size); void *mem = calloc(nmemb, size);
#ifdef REAL_WITH_MEMORY_TRACKING #ifdef REAL_WITH_MEMORY_TRACKING
memcount += malloc_usable_size(mem); if(mem){
if(memcount > max_memcount){ memcount += malloc_usable_size(mem);
max_memcount = memcount; if(memcount > max_memcount){
max_memcount = memcount;
}
} }
#endif #endif
@ -67,9 +69,11 @@ void *mosquitto__malloc(size_t size)
void *mem = malloc(size); void *mem = malloc(size);
#ifdef REAL_WITH_MEMORY_TRACKING #ifdef REAL_WITH_MEMORY_TRACKING
memcount += malloc_usable_size(mem); if(mem){
if(memcount > max_memcount){ memcount += malloc_usable_size(mem);
max_memcount = memcount; if(memcount > max_memcount){
max_memcount = memcount;
}
} }
#endif #endif
@ -99,9 +103,11 @@ void *mosquitto__realloc(void *ptr, size_t size)
mem = realloc(ptr, size); mem = realloc(ptr, size);
#ifdef REAL_WITH_MEMORY_TRACKING #ifdef REAL_WITH_MEMORY_TRACKING
memcount += malloc_usable_size(mem); if(mem){
if(memcount > max_memcount){ memcount += malloc_usable_size(mem);
max_memcount = memcount; if(memcount > max_memcount){
max_memcount = memcount;
}
} }
#endif #endif
@ -113,9 +119,11 @@ char *mosquitto__strdup(const char *s)
char *str = strdup(s); char *str = strdup(s);
#ifdef REAL_WITH_MEMORY_TRACKING #ifdef REAL_WITH_MEMORY_TRACKING
memcount += malloc_usable_size(str); if(str){
if(memcount > max_memcount){ memcount += malloc_usable_size(str);
max_memcount = memcount; if(memcount > max_memcount){
max_memcount = memcount;
}
} }
#endif #endif

@ -233,14 +233,11 @@ int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, cons
{ {
if(!mosq) return MOSQ_ERR_INVAL; if(!mosq) return MOSQ_ERR_INVAL;
if(mosq->username){ mosquitto__free(mosq->username);
mosquitto__free(mosq->username); mosq->username = NULL;
mosq->username = NULL;
} mosquitto__free(mosq->password);
if(mosq->password){ mosq->password = NULL;
mosquitto__free(mosq->password);
mosq->password = NULL;
}
if(username){ if(username){
mosq->username = mosquitto__strdup(username); mosq->username = mosquitto__strdup(username);
@ -319,30 +316,23 @@ void mosquitto__destroy(struct mosquitto *mosq)
mosquitto__free(mosq->tls_psk_identity); mosquitto__free(mosq->tls_psk_identity);
#endif #endif
if(mosq->address){ mosquitto__free(mosq->address);
mosquitto__free(mosq->address); mosq->address = NULL;
mosq->address = NULL;
} mosquitto__free(mosq->id);
if(mosq->id){ mosq->id = NULL;
mosquitto__free(mosq->id);
mosq->id = NULL; mosquitto__free(mosq->username);
} mosq->username = NULL;
if(mosq->username){
mosquitto__free(mosq->username); mosquitto__free(mosq->password);
mosq->username = NULL; mosq->password = NULL;
}
if(mosq->password){ mosquitto__free(mosq->host);
mosquitto__free(mosq->password); mosq->host = NULL;
mosq->password = NULL;
} mosquitto__free(mosq->bind_address);
if(mosq->host){ mosq->bind_address = NULL;
mosquitto__free(mosq->host);
mosq->host = NULL;
}
if(mosq->bind_address){
mosquitto__free(mosq->bind_address);
mosq->bind_address = NULL;
}
/* Out packet cleanup */ /* Out packet cleanup */
if(mosq->out_packet && !mosq->current_out_packet){ 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; if(!mosq || (!cafile && !capath) || (certfile && !keyfile) || (!certfile && keyfile)) return MOSQ_ERR_INVAL;
mosquitto__free(mosq->tls_cafile);
mosq->tls_cafile = NULL;
if(cafile){ if(cafile){
fptr = mosquitto__fopen(cafile, "rt"); fptr = mosquitto__fopen(cafile, "rt");
if(fptr){ if(fptr){
@ -648,71 +640,58 @@ int mosquitto_tls_set(struct mosquitto *mosq, const char *cafile, const char *ca
if(!mosq->tls_cafile){ if(!mosq->tls_cafile){
return MOSQ_ERR_NOMEM; 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){ if(capath){
mosq->tls_capath = mosquitto__strdup(capath); mosq->tls_capath = mosquitto__strdup(capath);
if(!mosq->tls_capath){ if(!mosq->tls_capath){
return MOSQ_ERR_NOMEM; 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){ if(certfile){
fptr = mosquitto__fopen(certfile, "rt"); fptr = mosquitto__fopen(certfile, "rt");
if(fptr){ if(fptr){
fclose(fptr); fclose(fptr);
}else{ }else{
if(mosq->tls_cafile){ mosquitto__free(mosq->tls_cafile);
mosquitto__free(mosq->tls_cafile); mosq->tls_cafile = NULL;
mosq->tls_cafile = NULL;
} mosquitto__free(mosq->tls_capath);
if(mosq->tls_capath){ mosq->tls_capath = NULL;
mosquitto__free(mosq->tls_capath);
mosq->tls_capath = NULL;
}
return MOSQ_ERR_INVAL; return MOSQ_ERR_INVAL;
} }
mosq->tls_certfile = mosquitto__strdup(certfile); mosq->tls_certfile = mosquitto__strdup(certfile);
if(!mosq->tls_certfile){ if(!mosq->tls_certfile){
return MOSQ_ERR_NOMEM; 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){ if(keyfile){
fptr = mosquitto__fopen(keyfile, "rt"); fptr = mosquitto__fopen(keyfile, "rt");
if(fptr){ if(fptr){
fclose(fptr); fclose(fptr);
}else{ }else{
if(mosq->tls_cafile){ mosquitto__free(mosq->tls_cafile);
mosquitto__free(mosq->tls_cafile); mosq->tls_cafile = NULL;
mosq->tls_cafile = NULL;
} mosquitto__free(mosq->tls_capath);
if(mosq->tls_capath){ mosq->tls_capath = NULL;
mosquitto__free(mosq->tls_capath);
mosq->tls_capath = NULL; mosquitto__free(mosq->tls_certfile);
} mosq->tls_certfile = NULL;
if(mosq->tls_certfile){
mosquitto__free(mosq->tls_certfile);
mosq->tls_certfile = NULL;
}
return MOSQ_ERR_INVAL; return MOSQ_ERR_INVAL;
} }
mosq->tls_keyfile = mosquitto__strdup(keyfile); mosq->tls_keyfile = mosquitto__strdup(keyfile);
if(!mosq->tls_keyfile){ if(!mosq->tls_keyfile){
return MOSQ_ERR_NOMEM; return MOSQ_ERR_NOMEM;
} }
}else{
mosquitto__free(mosq->tls_keyfile);
mosq->tls_keyfile = NULL;
} }
mosq->tls_pw_callback = pw_callback; mosq->tls_pw_callback = pw_callback;

@ -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(!host || strlen(host) > 256) return MOSQ_ERR_INVAL;
if(port < 1 || port > 65535) 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); mosq->socks5_host = mosquitto__strdup(host);
if(!mosq->socks5_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; mosq->socks5_port = port;
if(mosq->socks5_username){ mosquitto__free(mosq->socks5_username);
mosquitto__free(mosq->socks5_username); mosq->socks5_username = NULL;
}
if(mosq->socks5_password){ mosquitto__free(mosq->socks5_password);
mosquitto__free(mosq->socks5_password); mosq->socks5_password = NULL;
}
if(username){ if(username){
mosq->socks5_username = mosquitto__strdup(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){ if(password){
mosq->socks5_password = mosquitto__strdup(password); mosq->socks5_password = mosquitto__strdup(password);
if(!mosq->socks5_password){ mosquitto__free(mosq->socks5_username);
mosquitto__free(mosq->socks5_username); return MOSQ_ERR_NOMEM;
return MOSQ_ERR_NOMEM;
}
} }
} }

@ -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(mosquitto_pub_topic_check(topic)) return MOSQ_ERR_INVAL;
if(mosq->will){ if(mosq->will){
if(mosq->will->topic){ mosquitto__free(mosq->will->topic);
mosquitto__free(mosq->will->topic); mosquitto__free(mosq->will->payload);
mosq->will->topic = NULL;
}
if(mosq->will->payload){
mosquitto__free(mosq->will->payload);
mosq->will->payload = NULL;
}
mosquitto__free(mosq->will); mosquitto__free(mosq->will);
mosq->will = NULL;
} }
mosq->will = mosquitto__calloc(1, sizeof(struct mosquitto_message)); mosq->will = mosquitto__calloc(1, sizeof(struct mosquitto_message));
@ -81,9 +74,10 @@ cleanup:
if(mosq->will){ if(mosq->will){
mosquitto__free(mosq->will->topic); mosquitto__free(mosq->will->topic);
mosquitto__free(mosq->will->payload); mosquitto__free(mosq->will->payload);
mosquitto__free(mosq->will);
mosq->will = NULL;
} }
mosquitto__free(mosq->will);
mosq->will = NULL;
return rc; return rc;
} }
@ -92,14 +86,12 @@ int will__clear(struct mosquitto *mosq)
{ {
if(!mosq->will) return MOSQ_ERR_SUCCESS; if(!mosq->will) return MOSQ_ERR_SUCCESS;
if(mosq->will->topic){ mosquitto__free(mosq->will->topic);
mosquitto__free(mosq->will->topic); mosq->will->topic = NULL;
mosq->will->topic = NULL;
} mosquitto__free(mosq->will->payload);
if(mosq->will->payload){ mosq->will->payload = NULL;
mosquitto__free(mosq->will->payload);
mosq->will->payload = NULL;
}
mosquitto__free(mosq->will); mosquitto__free(mosq->will);
mosq->will = NULL; mosq->will = NULL;

@ -124,10 +124,9 @@ static void config__init_reload(struct mosquitto__config *config)
fclose(config->log_fptr); fclose(config->log_fptr);
config->log_fptr = NULL; config->log_fptr = NULL;
} }
if(config->log_file){ mosquitto__free(config->log_file);
mosquitto__free(config->log_file); config->log_file = NULL;
config->log_file = NULL;
}
#if defined(WIN32) || defined(__CYGWIN__) #if defined(WIN32) || defined(__CYGWIN__)
if(service_handle){ if(service_handle){
/* This is running as a Windows service. Default to no logging. Using /* 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){ if(config->auth_plugins){
for(i=0; i<config->auth_plugin_count; i++){ for(i=0; i<config->auth_plugin_count; i++){
plug = &config->auth_plugins[i]; plug = &config->auth_plugins[i];
if(plug->path){ mosquitto__free(plug->path);
mosquitto__free(plug->path); plug->path = NULL;
plug->path = NULL;
}
if(plug->options){ if(plug->options){
for(j=0; j<plug->option_count; j++){ for(j=0; j<plug->option_count; j++){
mosquitto__free(plug->options[j].key); mosquitto__free(plug->options[j].key);
@ -302,10 +300,8 @@ void config__cleanup(struct mosquitto__config *config)
if(config->auth_plugins){ if(config->auth_plugins){
for(i=0; i<config->auth_plugin_count; i++){ for(i=0; i<config->auth_plugin_count; i++){
plug = &config->auth_plugins[i]; plug = &config->auth_plugins[i];
if(plug->path){ mosquitto__free(plug->path);
mosquitto__free(plug->path); plug->path = NULL;
plug->path = NULL;
}
if(plug->options){ if(plug->options){
for(j=0; j<plug->option_count; j++){ for(j=0; j<plug->option_count; j++){
mosquitto__free(plug->options[j].key); 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"); config->persistence_file = mosquitto__strdup("mosquitto.db");
if(!config->persistence_file) return MOSQ_ERR_NOMEM; 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)){ if(config->persistence_location && strlen(config->persistence_location)){
len = strlen(config->persistence_location) + strlen(config->persistence_file) + 1; len = strlen(config->persistence_location) + strlen(config->persistence_file) + 1;
config->persistence_filepath = mosquitto__malloc(len); 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(token){
if(!strcmp(token, "acl_file")){ if(!strcmp(token, "acl_file")){
if(reload){ if(reload){
if(config->acl_file){ mosquitto__free(config->acl_file);
mosquitto__free(config->acl_file); config->acl_file = NULL;
config->acl_file = NULL;
}
} }
if(conf__parse_string(&token, "acl_file", &config->acl_file, saveptr)) return MOSQ_ERR_INVAL; if(conf__parse_string(&token, "acl_file", &config->acl_file, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "address") || !strcmp(token, "addresses")){ }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 #endif
}else if(!strcmp(token, "clientid_prefixes")){ }else if(!strcmp(token, "clientid_prefixes")){
if(reload){ if(reload){
if(config->clientid_prefixes){ mosquitto__free(config->clientid_prefixes);
mosquitto__free(config->clientid_prefixes); config->clientid_prefixes = NULL;
config->clientid_prefixes = NULL;
}
} }
if(conf__parse_string(&token, "clientid_prefixes", &config->clientid_prefixes, saveptr)) return MOSQ_ERR_INVAL; if(conf__parse_string(&token, "clientid_prefixes", &config->clientid_prefixes, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "connection")){ }else if(!strcmp(token, "connection")){
@ -1535,10 +1525,8 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, const
#endif #endif
}else if(!strcmp(token, "password_file")){ }else if(!strcmp(token, "password_file")){
if(reload){ if(reload){
if(config->password_file){ mosquitto__free(config->password_file);
mosquitto__free(config->password_file); config->password_file = NULL;
config->password_file = NULL;
}
} }
if(conf__parse_string(&token, "password_file", &config->password_file, saveptr)) return MOSQ_ERR_INVAL; if(conf__parse_string(&token, "password_file", &config->password_file, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "persistence") || !strcmp(token, "retained_persistence")){ }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")){ }else if(!strcmp(token, "psk_file")){
#ifdef REAL_WITH_TLS_PSK #ifdef REAL_WITH_TLS_PSK
if(reload){ if(reload){
if(config->psk_file){ mosquitto__free(config->psk_file);
mosquitto__free(config->psk_file); config->psk_file = NULL;
config->psk_file = NULL;
}
} }
if(conf__parse_string(&token, "psk_file", &config->psk_file, saveptr)) return MOSQ_ERR_INVAL; if(conf__parse_string(&token, "psk_file", &config->psk_file, saveptr)) return MOSQ_ERR_INVAL;
#else #else

@ -98,14 +98,12 @@ void context__cleanup(struct mosquitto_db *db, struct mosquitto *context, bool d
if(!context) return; if(!context) return;
if(context->username){ mosquitto__free(context->username);
mosquitto__free(context->username); context->username = NULL;
context->username = NULL;
} mosquitto__free(context->password);
if(context->password){ context->password = NULL;
mosquitto__free(context->password);
context->password = NULL;
}
#ifdef WITH_BRIDGE #ifdef WITH_BRIDGE
if(context->bridge){ if(context->bridge){
for(i=0; i<db->bridge_count; i++){ for(i=0; i<db->bridge_count; i++){
@ -137,10 +135,9 @@ void context__cleanup(struct mosquitto_db *db, struct mosquitto *context, bool d
sub__clean_session(db, context); sub__clean_session(db, context);
db__messages_delete(db, context); db__messages_delete(db, context);
} }
if(context->address){
mosquitto__free(context->address); mosquitto__free(context->address);
context->address = NULL; context->address = NULL;
}
if(context->id){ if(context->id){
assert(db); /* db can only be NULL here if the client hasn't sent a assert(db); /* db can only be NULL here if the client hasn't sent a

@ -598,9 +598,7 @@ int db__message_store(struct mosquitto_db *db, const char *source, uint16_t sour
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
error: error:
if(topic){ mosquitto__free(topic);
mosquitto__free(topic);
}
if(temp){ if(temp){
mosquitto__free(temp->source_id); mosquitto__free(temp->source_id);
mosquitto__free(temp->topic); mosquitto__free(temp->topic);

@ -386,9 +386,7 @@ int main(int argc, char *argv[])
if(int_db.config->listeners[i].ws_context){ if(int_db.config->listeners[i].ws_context){
libwebsocket_context_destroy(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 #endif
@ -410,9 +408,7 @@ int main(int argc, char *argv[])
context__cleanup(&int_db, int_db.bridges[i], true); context__cleanup(&int_db, int_db.bridges[i], true);
} }
} }
if(int_db.bridges){ mosquitto__free(int_db.bridges);
mosquitto__free(int_db.bridges);
}
#endif #endif
context__free_disused(&int_db); context__free_disused(&int_db);

@ -439,9 +439,7 @@ static void free__acl(struct mosquitto__acl *acl)
if(acl->next){ if(acl->next){
free__acl(acl->next); free__acl(acl->next);
} }
if(acl->topic){ mosquitto__free(acl->topic);
mosquitto__free(acl->topic);
}
mosquitto__free(acl); mosquitto__free(acl);
} }
@ -467,9 +465,7 @@ static int acl__cleanup(struct mosquitto_db *db, bool reload)
user_tail = db->acl_list->next; user_tail = db->acl_list->next;
free__acl(db->acl_list->acl); 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); mosquitto__free(db->acl_list);
db->acl_list = user_tail; db->acl_list = user_tail;

Loading…
Cancel
Save