From 3fd1dc477fd5a817059c34207f2840d83a2defaf Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 3 Jul 2014 21:55:25 +0100 Subject: [PATCH] Fix defects identified by Coverity. --- src/bridge.c | 3 ++- src/read_handle_server.c | 13 +++++-------- src/websockets.c | 11 +++++++++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/bridge.c b/src/bridge.c index 3830eb64..c87d46f5 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -80,13 +80,14 @@ int mqtt3_bridge_new(struct mosquitto_db *db, struct _mqtt3_bridge *bridge) HASH_FIND(hh_id, db->contexts_by_id, local_id, strlen(local_id), new_context); if(new_context){ /* (possible from persistent db) */ + _mosquitto_free(local_id); }else{ /* id wasn't found, so generate a new context */ new_context = mqtt3_context_init(db, -1); if(!new_context){ return MOSQ_ERR_NOMEM; } - new_context->id = _mosquitto_strdup(local_id); + new_context->id = local_id; HASH_ADD_KEYPTR(hh_id, db->contexts_by_id, new_context->id, strlen(new_context->id), new_context); } new_context->bridge = bridge; diff --git a/src/read_handle_server.c b/src/read_handle_server.c index a005ce29..3f9a14c4 100644 --- a/src/read_handle_server.c +++ b/src/read_handle_server.c @@ -468,6 +468,7 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context) ws_ctxt_user_head = (struct libws_mqtt_hack *)libwebsocket_context_user(found_context->ws_context); ws_ctxt_user = _mosquitto_calloc(1, sizeof(struct libws_mqtt_hack)); if(!ws_ctxt_user){ + rc = MOSQ_ERR_NOMEM; goto handle_connect_error; } ws_ctxt_user->old_mosq = found_context; @@ -492,20 +493,16 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context) ws_ctxt_user_head = (struct libws_mqtt_hack *)libwebsocket_context_user(found_context->ws_context); ws_ctxt_user = _mosquitto_calloc(1, sizeof(struct libws_mqtt_hack)); if(!ws_ctxt_user){ + rc = MOSQ_ERR_NOMEM; goto handle_connect_error; } ws_ctxt_user->old_mosq = context; ws_ctxt_user->new_mosq = found_context; - if(ws_ctxt_user_head){ - while(ws_ctxt_user_head->next){ - ws_ctxt_user_head = ws_ctxt_user_head->next; - } - ws_ctxt_user_head->next = ws_ctxt_user; - }else{ - ws_ctxt_user->next = ws_ctxt_user_head->next; - ws_ctxt_user_head->next = ws_ctxt_user; + while(ws_ctxt_user_head->next){ + ws_ctxt_user_head = ws_ctxt_user_head->next; } + ws_ctxt_user_head->next = ws_ctxt_user; HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, context, sizeof(void *), context); }else{ HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, context, sizeof(void *), context); diff --git a/src/websockets.c b/src/websockets.c index 35ab898e..c217049b 100644 --- a/src/websockets.c +++ b/src/websockets.c @@ -141,10 +141,15 @@ static int callback_mqtt(struct libwebsocket_context *context, mosq->ws_context = context; mosq->wsi = wsi; u->mosq = mosq; + }else{ + return -1; } break; case LWS_CALLBACK_CLOSED: + if(!u){ + return -1; + } mosq = u->mosq; if(mosq){ mosq->wsi = NULL; @@ -153,6 +158,9 @@ static int callback_mqtt(struct libwebsocket_context *context, break; case LWS_CALLBACK_SERVER_WRITEABLE: + if(!u){ + return -1; + } mosq = u->mosq; if(!mosq || mosq->state == mosq_cs_disconnect_ws || mosq->state == mosq_cs_disconnecting){ return -1; @@ -209,6 +217,9 @@ static int callback_mqtt(struct libwebsocket_context *context, break; case LWS_CALLBACK_RECEIVE: + if(!u){ + return -1; + } mosq = u->mosq; pos = 0; buf = (uint8_t *)in;