Skip to content

Commit c588263

Browse files
mdns: Fix minor memory leaks when creating services
* Original commit: espressif/esp-idf@fad62cc
1 parent 6258edf commit c588263

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

components/mdns/mdns.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,11 @@ static mdns_txt_linked_item_t * _mdns_allocate_txt(size_t num_items, mdns_txt_it
22652265
}
22662266
return new_txt;
22672267
}
2268+
2269+
/**
2270+
* @brief Deallocate the txt linked list
2271+
* @param txt pointer to the txt pointer to free, noop if txt==NULL
2272+
*/
22682273
static void _mdns_free_linked_txt(mdns_txt_linked_item_t *txt)
22692274
{
22702275
mdns_txt_linked_item_t *t;
@@ -2293,16 +2298,15 @@ static mdns_service_t * _mdns_create_service(const char * service, const char *
22932298
uint16_t port, const char * instance, size_t num_items,
22942299
mdns_txt_item_t txt[])
22952300
{
2296-
mdns_service_t * s = (mdns_service_t *)malloc(sizeof(mdns_service_t));
2301+
mdns_service_t * s = (mdns_service_t *)calloc(1, sizeof(mdns_service_t));
22972302
if (!s) {
22982303
HOOK_MALLOC_FAILED;
22992304
return NULL;
23002305
}
23012306

23022307
mdns_txt_linked_item_t * new_txt = _mdns_allocate_txt(num_items, txt);
23032308
if (num_items && new_txt == NULL) {
2304-
free(s);
2305-
return NULL;
2309+
goto fail;
23062310
}
23072311

23082312
s->priority = 0;
@@ -2328,19 +2332,16 @@ static mdns_service_t * _mdns_create_service(const char * service, const char *
23282332

23292333
s->proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
23302334
if (!s->proto) {
2331-
free((char *)s->service);
23322335
goto fail;
23332336
}
2334-
23352337
return s;
23362338

23372339
fail:
2338-
if (s->instance) {
2339-
free(s->instance);
2340-
}
2341-
if (s->hostname) {
2342-
free(s->hostname);
2343-
}
2340+
_mdns_free_linked_txt(s->txt);
2341+
free((char *)s->instance);
2342+
free((char *)s->service);
2343+
free((char *)s->proto);
2344+
free((char *)s->hostname);
23442345
free(s);
23452346

23462347
return NULL;

0 commit comments

Comments
 (0)