Skip to content

Commit b4e5742

Browse files
mdns: resolve memory leak when txt record received multiple times
* Original commit: espressif/esp-idf@a6b2b73
1 parent 2763bcd commit b4e5742

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

components/mdns/mdns.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,10 +2769,6 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
27692769
if (search_result) {
27702770
mdns_txt_item_t * txt = NULL;
27712771
size_t txt_count = 0;
2772-
_mdns_result_txt_create(data_ptr, data_len, &txt, &txt_count);
2773-
if (!txt_count) {
2774-
continue;
2775-
}
27762772

27772773
mdns_result_t * result = NULL;
27782774
if (search_result->type == MDNS_TYPE_PTR) {
@@ -2792,8 +2788,11 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
27922788
}
27932789
}
27942790
if (!result->txt) {
2795-
result->txt = txt;
2796-
result->txt_count = txt_count;
2791+
_mdns_result_txt_create(data_ptr, data_len, &txt, &txt_count);
2792+
if (txt_count) {
2793+
result->txt = txt;
2794+
result->txt_count = txt_count;
2795+
}
27972796
}
27982797
} else {
27992798
_mdns_search_result_add_txt(search_result, txt, txt_count, packet->tcpip_if, packet->ip_protocol);
@@ -3305,7 +3304,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
33053304
while (r) {
33063305
if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) {
33073306
if (r->txt) {
3308-
return;
3307+
goto free_txt;
33093308
}
33103309
r->txt = txt;
33113310
r->txt_count = txt_count;
@@ -3316,12 +3315,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
33163315
if (!search->max_results || search->num_results < search->max_results) {
33173316
r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
33183317
if (!r) {
3319-
for (i=0; i<txt_count; i++) {
3320-
free((char *)(txt[i].key));
3321-
free((char *)(txt[i].value));
3322-
}
3323-
free(txt);
3324-
return;
3318+
goto free_txt;
33253319
}
33263320

33273321
memset(r, 0 , sizeof(mdns_result_t));
@@ -3333,6 +3327,14 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
33333327
search->result = r;
33343328
search->num_results++;
33353329
}
3330+
return;
3331+
3332+
free_txt:
3333+
for (i=0; i<txt_count; i++) {
3334+
free((char *)(txt[i].key));
3335+
free((char *)(txt[i].value));
3336+
}
3337+
free(txt);
33363338
}
33373339

33383340
/**

0 commit comments

Comments
 (0)