Skip to content

Commit

Permalink
mdns: resolve memory leak when txt record received multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Dec 11, 2018
1 parent 31163f0 commit a6b2b73
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions components/mdns/mdns.c
Expand Up @@ -2769,10 +2769,6 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
if (search_result) {
mdns_txt_item_t * txt = NULL;
size_t txt_count = 0;
_mdns_result_txt_create(data_ptr, data_len, &txt, &txt_count);
if (!txt_count) {
continue;
}

mdns_result_t * result = NULL;
if (search_result->type == MDNS_TYPE_PTR) {
Expand All @@ -2792,8 +2788,11 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
}
}
if (!result->txt) {
result->txt = txt;
result->txt_count = txt_count;
_mdns_result_txt_create(data_ptr, data_len, &txt, &txt_count);
if (txt_count) {
result->txt = txt;
result->txt_count = txt_count;
}
}
} else {
_mdns_search_result_add_txt(search_result, txt, txt_count, packet->tcpip_if, packet->ip_protocol);
Expand Down Expand Up @@ -3305,7 +3304,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
while (r) {
if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) {
if (r->txt) {
return;
goto free_txt;
}
r->txt = txt;
r->txt_count = txt_count;
Expand All @@ -3316,12 +3315,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
if (!search->max_results || search->num_results < search->max_results) {
r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
if (!r) {
for (i=0; i<txt_count; i++) {
free((char *)(txt[i].key));
free((char *)(txt[i].value));
}
free(txt);
return;
goto free_txt;
}

memset(r, 0 , sizeof(mdns_result_t));
Expand All @@ -3333,6 +3327,14 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
search->result = r;
search->num_results++;
}
return;

free_txt:
for (i=0; i<txt_count; i++) {
free((char *)(txt[i].key));
free((char *)(txt[i].value));
}
free(txt);
}

/**
Expand Down

0 comments on commit a6b2b73

Please sign in to comment.