From 9faad1e078159b0530fc23fc6647268ac4a5ed84 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 26 Mar 2019 14:00:42 +0000 Subject: [PATCH] Fix a variety of scan-build warnings. --- src/conf.c | 2 +- src/handle_connect.c | 5 ++--- src/loop.c | 9 +++------ src/mosquitto_passwd.c | 5 ++++- src/net.c | 1 + src/persist_read.c | 4 +--- src/persist_read_v5.c | 3 ++- src/subs.c | 13 ++++--------- 8 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/conf.c b/src/conf.c index ad27062d..0204c7aa 100644 --- a/src/conf.c +++ b/src/conf.c @@ -758,7 +758,7 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct char *key; struct mosquitto__listener *cur_listener = &config->default_listener; int i; - int lineno_ext; + int lineno_ext = 0; *lineno = 0; diff --git a/src/handle_connect.c b/src/handle_connect.c index 39c397ba..d5aa8ab0 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -444,7 +444,6 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) }else if(rc == MOSQ_ERR_PROTOCOL){ if(context->protocol == mosq_p_mqtt31){ /* Password flag given, but no password. Ignore. */ - password_flag = 0; }else{ rc = MOSQ_ERR_PROTOCOL; goto handle_connect_error; @@ -480,7 +479,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) } #ifdef WITH_TLS - if(context->listener && context->listener->ssl_ctx && (context->listener->use_identity_as_username || context->listener->use_subject_as_username)){ + if(context->listener->ssl_ctx && (context->listener->use_identity_as_username || context->listener->use_subject_as_username)){ /* Don't need the username or password if provided */ mosquitto__free(username); username = NULL; @@ -615,7 +614,7 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) } #endif - if(context->listener && context->listener->use_username_as_clientid){ + if(context->listener->use_username_as_clientid){ if(context->username){ mosquitto__free(client_id); client_id = mosquitto__strdup(context->username); diff --git a/src/loop.c b/src/loop.c index a37885bb..02081be1 100644 --- a/src/loop.c +++ b/src/loop.c @@ -110,7 +110,6 @@ int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int li time_t last_backup = mosquitto_time(); #endif time_t now = 0; - time_t now_time; int time_count; int fdcount; struct mosquitto *context, *ctxt_tmp; @@ -216,8 +215,6 @@ int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int li } #endif - now_time = time(NULL); - time_count = 0; HASH_ITER(hh_sock, db->contexts_by_sock, context, ctxt_tmp){ if(time_count > 0){ @@ -469,8 +466,8 @@ int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int li } } #endif - now_time = time(NULL); - if(db->config->persistent_client_expiration > 0 && now_time > expiration_check_time){ + now = time(NULL); + if(db->config->persistent_client_expiration > 0 && now > expiration_check_time){ HASH_ITER(hh_id, db->contexts_by_id, context, ctxt_tmp){ if(context->sock == INVALID_SOCKET && context->session_expiry_interval > 0 && context->session_expiry_interval != UINT32_MAX){ /* This is a persistent client, check to see if the @@ -478,7 +475,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, mosq_sock_t *listensock, int li * persistent_client_expiration seconds ago. If so, * expire it and clean up. */ - if(now_time > context->session_expiry_time){ + if(now > context->session_expiry_time){ if(context->id){ id = context->id; }else{ diff --git a/src/mosquitto_passwd.c b/src/mosquitto_passwd.c index 3dda8aca..f0e5853d 100644 --- a/src/mosquitto_passwd.c +++ b/src/mosquitto_passwd.c @@ -527,7 +527,10 @@ int main(int argc, char *argv[]) if(create_new){ rc = get_password(password, 1024); - if(rc) return rc; + if(rc){ + free(password_file); + return rc; + } fptr = fopen(password_file, "wt"); if(!fptr){ fprintf(stderr, "Error: Unable to open file %s for writing. %s.\n", password_file, strerror(errno)); diff --git a/src/net.c b/src/net.c index fe569b65..6d72b503 100644 --- a/src/net.c +++ b/src/net.c @@ -666,6 +666,7 @@ int net__socket_get_address(mosq_sock_t sock, char *buf, int len) struct sockaddr_storage addr; socklen_t addrlen; + memset(&addr, 0, sizeof(struct sockaddr_storage)); addrlen = sizeof(addr); if(!getpeername(sock, (struct sockaddr *)&addr, &addrlen)){ if(addr.ss_family == AF_INET){ diff --git a/src/persist_read.c b/src/persist_read.c index 4bfe0273..30f43876 100644 --- a/src/persist_read.c +++ b/src/persist_read.c @@ -271,15 +271,13 @@ static int persist__msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fp chunk.topic, chunk.F.qos, chunk.F.payloadlen, &chunk.payload, chunk.F.retain, &stored, message_expiry_interval, chunk.properties, chunk.F.store_id); - if(stored){ - stored->source_listener = chunk.source.listener; - } mosquitto__free(chunk.source.id); mosquitto__free(chunk.source.username); chunk.source.id = NULL; chunk.source.username = NULL; if(rc == MOSQ_ERR_SUCCESS){ + stored->source_listener = chunk.source.listener; load->db_id = stored->db_id; load->store = stored; diff --git a/src/persist_read_v5.c b/src/persist_read_v5.c index 286eae2e..ba851687 100644 --- a/src/persist_read_v5.c +++ b/src/persist_read_v5.c @@ -131,6 +131,8 @@ int persist__chunk_msg_store_read_v5(FILE *db_fptr, struct P_msg_store *chunk, u mosquitto_property *properties = NULL; struct mosquitto__packet prop_packet; + memset(&prop_packet, 0, sizeof(struct mosquitto__packet)); + read_e(db_fptr, &chunk->F, sizeof(struct PF_msg_store)); chunk->F.payloadlen = ntohl(chunk->F.payloadlen); chunk->F.source_mid = ntohs(chunk->F.source_mid); @@ -179,7 +181,6 @@ int persist__chunk_msg_store_read_v5(FILE *db_fptr, struct P_msg_store *chunk, u } if(length > 0){ - memset(&prop_packet, 0, sizeof(struct mosquitto__packet)); prop_packet.remaining_length = length; prop_packet.payload = mosquitto__malloc(length); if(!prop_packet.payload){ diff --git a/src/subs.c b/src/subs.c index 1c391fed..53e28820 100644 --- a/src/subs.c +++ b/src/subs.c @@ -162,7 +162,7 @@ static int subs__process(struct mosquitto_db *db, struct mosquitto__subhier *hie } } - rc2 = subs__shared_process(db, hier, topic, qos, retain, stored); + rc = subs__shared_process(db, hier, topic, qos, retain, stored); leaf = hier->subs; while(source_id && leaf){ @@ -307,9 +307,8 @@ static int sub__add_leaf(struct mosquitto *context, int qos, uint32_t identifier } leaf = leaf->next; } - leaf = mosquitto__malloc(sizeof(struct mosquitto__subleaf)); + leaf = mosquitto__calloc(1, sizeof(struct mosquitto__subleaf)); if(!leaf) return MOSQ_ERR_NOMEM; - leaf->next = NULL; leaf->context = context; leaf->qos = qos; leaf->identifier = identifier; @@ -413,7 +412,7 @@ static int sub__add_shared(struct mosquitto_db *db, struct mosquitto *context, i static int sub__add_normal(struct mosquitto_db *db, struct mosquitto *context, int qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier) { - struct mosquitto__subleaf *newleaf; + struct mosquitto__subleaf *newleaf = NULL; struct mosquitto__subhier **subs; int i; int rc; @@ -668,7 +667,7 @@ struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent assert(sibling); - child = mosquitto__malloc(sizeof(struct mosquitto__subhier)); + child = mosquitto__calloc(1, sizeof(struct mosquitto__subhier)); if(!child){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); return NULL; @@ -684,10 +683,6 @@ struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent }else{ strncpy(child->topic, topic, child->topic_len+1); } - child->subs = NULL; - child->shared = NULL; - child->children = NULL; - child->retained = NULL; HASH_ADD_KEYPTR(hh, *sibling, child->topic, child->topic_len, child);