Skip to content

Commit

Permalink
Various memory fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Jun 28, 2014
1 parent ff5fd26 commit 15efd2d
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 66 deletions.
5 changes: 5 additions & 0 deletions lib/net_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ and the Eclipse Distribution License is available at
#endif

#ifdef WITH_TLS
#include <openssl/conf.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <tls_mosq.h>
#endif
Expand Down Expand Up @@ -107,6 +109,9 @@ void _mosquitto_net_init(void)
void _mosquitto_net_cleanup(void)
{
#ifdef WITH_TLS
ERR_remove_state(0);
ENGINE_cleanup();
CONF_modules_unload(1);
ERR_free_strings();
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
Expand Down
4 changes: 2 additions & 2 deletions src/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int mqtt3_bridge_new(struct mosquitto_db *db, struct _mqtt3_bridge *bridge)
bridge->clientid = id;
}
if(bridge->local_clientid){
local_id = bridge->local_clientid;
local_id = _mosquitto_strdup(bridge->local_clientid);
}else{
len = strlen(bridge->clientid) + strlen("local.") + 2;
local_id = _mosquitto_malloc(len);
Expand Down Expand Up @@ -111,7 +111,7 @@ int mqtt3_bridge_new(struct mosquitto_db *db, struct _mqtt3_bridge *bridge)

bridge->try_private_accepted = true;

HASH_ADD_KEYPTR(hh_bridge, db->contexts_bridge, new_context->id, strlen(new_context->id), new_context);
HASH_ADD_KEYPTR(hh_bridge, db->contexts_bridge, new_context->bridge->local_clientid, strlen(new_context->bridge->local_clientid), new_context);

return mqtt3_bridge_connect(db, new_context);
}
Expand Down
4 changes: 4 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,13 @@ void mqtt3_config_cleanup(struct mqtt3_config *config)
_mosquitto_free(config->bridges[i].topics);
}
if(config->bridges[i].notification_topic) _mosquitto_free(config->bridges[i].notification_topic);
#ifdef WITH_TLS
if(config->bridges[i].tls_version) _mosquitto_free(config->bridges[i].tls_version);
if(config->bridges[i].tls_cafile) _mosquitto_free(config->bridges[i].tls_cafile);
#ifdef REAL_WITH_TLS_PSK
if(config->bridges[i].tls_psk_identity) _mosquitto_free(config->bridges[i].tls_psk_identity);
if(config->bridges[i].tls_psk) _mosquitto_free(config->bridges[i].tls_psk);
#endif
#endif
}
_mosquitto_free(config->bridges);
Expand Down
18 changes: 18 additions & 0 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b
{
struct _mosquitto_packet *packet;
struct mosquitto_client_msg *msg, *next;
#ifdef WITH_BRIDGE
struct mosquitto *ctx_tmp;
#endif

if(!context) return;

Expand All @@ -105,12 +108,27 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b
}
#ifdef WITH_BRIDGE
if(context->bridge){
if(context->bridge->local_clientid){
HASH_FIND(hh_bridge, db->contexts_bridge, context->bridge->local_clientid, strlen(context->bridge->local_clientid), ctx_tmp);
if(ctx_tmp){
HASH_DELETE(hh_bridge, db->contexts_bridge, context);
}
}
if(context->bridge->username){
context->bridge->username = NULL;
}
if(context->bridge->password){
context->bridge->password = NULL;
}
if(context->bridge->local_username){
context->bridge->local_username = NULL;
}
if(context->bridge->local_password){
context->bridge->local_password = NULL;
}
if(context->bridge->local_clientid){
context->bridge->local_clientid = NULL;
}
}
#endif
_mosquitto_socket_close(db, context);
Expand Down
14 changes: 11 additions & 3 deletions src/mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,16 @@ int main(int argc, char *argv[])
HASH_ITER(hh_id, int_db.contexts_by_id, ctxt, ctxt_tmp){
mqtt3_context_cleanup(&int_db, ctxt, true);
}
HASH_CLEAR(hh_sock, int_db.contexts_by_sock);
HASH_CLEAR(hh_bridge, int_db.contexts_bridge);
HASH_ITER(hh_sock, int_db.contexts_by_sock, ctxt, ctxt_tmp){
mqtt3_context_cleanup(&int_db, ctxt, true);
}
HASH_ITER(hh_bridge, int_db.contexts_bridge, ctxt, ctxt_tmp){
mqtt3_context_cleanup(&int_db, ctxt, true);
}
HASH_ITER(hh_for_free, int_db.contexts_for_free, ctxt, ctxt_tmp){
HASH_DELETE(hh_for_free, int_db.contexts_for_free, ctxt);
mqtt3_context_cleanup(&int_db, ctxt, true);
}
mqtt3_db_close(&int_db);

if(listensock){
Expand All @@ -356,8 +364,8 @@ int main(int argc, char *argv[])
remove(config.pid_file);
}

_mosquitto_net_cleanup();
mqtt3_config_cleanup(int_db.config);
_mosquitto_net_cleanup();

return rc;
}
Expand Down
11 changes: 9 additions & 2 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,22 @@ static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned
if(!psk_key) return 0;

if(mosquitto_psk_key_get(db, psk_hint, identity, psk_key, max_psk_len*2) != MOSQ_ERR_SUCCESS){
_mosquitto_free(psk_key);
return 0;
}

len = _mosquitto_hex2bin(psk_key, psk, max_psk_len);
if (len < 0) return 0;
if (len < 0){
_mosquitto_free(psk_key);
return 0;
}

if(listener->use_identity_as_username){
context->username = _mosquitto_strdup(identity);
if(!context->username) return 0;
if(!context->username){
_mosquitto_free(psk_key);
return 0;
}
}

return len;
Expand Down
Loading

0 comments on commit 15efd2d

Please sign in to comment.