Memory leak in handle_unsubscribe.c

Reason: In line 70, the memory allocation for the pointer reasons_codes may
result to a memory leak due to the many returns (e.g as the one in line 78)
occuring in the program's path until reaching the mosquitto__free at line 122.

Fix: I added a mosquitto__free(reason_codes) statement before each return
statement that could result to a memory leak

Signed-off-by: Panagiotis Vasilikos <panagiotis.vasilikos@alexandra.dk>
pull/1575/head
Panagiotis Vasilikos 6 years ago
parent caeb211cc5
commit 49bf788862

@ -66,9 +66,16 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context)
} }
} }
reason_code_max = 10;
reason_codes = mosquitto__malloc(reason_code_max);
if(!reason_codes){
return MOSQ_ERR_NOMEM;
}
while(context->in_packet.pos < context->in_packet.remaining_length){ while(context->in_packet.pos < context->in_packet.remaining_length){
sub = NULL; sub = NULL;
if(packet__read_string(&context->in_packet, &sub, &slen)){ if(packet__read_string(&context->in_packet, &sub, &slen)){
mosquitto__free(reason_codes);
return 1; return 1;
} }
@ -77,6 +84,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context)
"Empty unsubscription string from %s, disconnecting.", "Empty unsubscription string from %s, disconnecting.",
context->id); context->id);
mosquitto__free(sub); mosquitto__free(sub);
mosquitto_free(reason_codes);
return 1; return 1;
} }
if(mosquitto_sub_topic_check(sub)){ if(mosquitto_sub_topic_check(sub)){
@ -84,6 +92,7 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context)
"Invalid unsubscription string from %s, disconnecting.", "Invalid unsubscription string from %s, disconnecting.",
context->id); context->id);
mosquitto__free(sub); mosquitto__free(sub);
mosquitto__free(reason_codes);
return 1; return 1;
} }
@ -91,12 +100,9 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context)
rc = sub__remove(db, context, sub, db->subs, &reason); rc = sub__remove(db, context, sub, db->subs, &reason);
log__printf(NULL, MOSQ_LOG_UNSUBSCRIBE, "%s %s", context->id, sub); log__printf(NULL, MOSQ_LOG_UNSUBSCRIBE, "%s %s", context->id, sub);
mosquitto__free(sub); mosquitto__free(sub);
if(rc) return rc; if(rc){
mosquitto_fee(reason_codes);
reason_code_max = 10; return rc;
reason_codes = mosquitto__malloc(reason_code_max);
if(!reason_codes){
return MOSQ_ERR_NOMEM;
} }
reason_codes[reason_code_count] = reason; reason_codes[reason_code_count] = reason;

Loading…
Cancel
Save