mdns: Fix minor memory leaks when creating services

This commit is contained in:
David Cermak 2022-01-17 08:31:16 +01:00
parent 119b4a9dd1
commit fad62cc1ed

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -2247,6 +2247,11 @@ static mdns_txt_linked_item_t * _mdns_allocate_txt(size_t num_items, mdns_txt_it
} }
return new_txt; return new_txt;
} }
/**
* @brief Deallocate the txt linked list
* @param txt pointer to the txt pointer to free, noop if txt==NULL
*/
static void _mdns_free_linked_txt(mdns_txt_linked_item_t *txt) static void _mdns_free_linked_txt(mdns_txt_linked_item_t *txt)
{ {
mdns_txt_linked_item_t *t; mdns_txt_linked_item_t *t;
@ -2274,7 +2279,7 @@ static mdns_service_t * _mdns_create_service(const char * service, const char *
uint16_t port, const char * instance, size_t num_items, uint16_t port, const char * instance, size_t num_items,
mdns_txt_item_t txt[]) mdns_txt_item_t txt[])
{ {
mdns_service_t * s = (mdns_service_t *)malloc(sizeof(mdns_service_t)); mdns_service_t * s = (mdns_service_t *)calloc(1, sizeof(mdns_service_t));
if (!s) { if (!s) {
HOOK_MALLOC_FAILED; HOOK_MALLOC_FAILED;
return NULL; return NULL;
@ -2282,8 +2287,7 @@ static mdns_service_t * _mdns_create_service(const char * service, const char *
mdns_txt_linked_item_t * new_txt = _mdns_allocate_txt(num_items, txt); mdns_txt_linked_item_t * new_txt = _mdns_allocate_txt(num_items, txt);
if (num_items && new_txt == NULL) { if (num_items && new_txt == NULL) {
free(s); goto fail;
return NULL;
} }
s->priority = 0; s->priority = 0;
@ -2309,19 +2313,16 @@ static mdns_service_t * _mdns_create_service(const char * service, const char *
s->proto = strndup(proto, MDNS_NAME_BUF_LEN - 1); s->proto = strndup(proto, MDNS_NAME_BUF_LEN - 1);
if (!s->proto) { if (!s->proto) {
free((char *)s->service);
goto fail; goto fail;
} }
return s; return s;
fail: fail:
if (s->instance) { _mdns_free_linked_txt(s->txt);
free(s->instance); free((char *)s->instance);
} free((char *)s->service);
if (s->hostname) { free((char *)s->proto);
free(s->hostname); free((char *)s->hostname);
}
free(s); free(s);
return NULL; return NULL;