Skip to content

Commit 7710ea9

Browse files
mdns: Fix memleak when adding delegated host
* Original commit: espressif/esp-idf@9cbdb87
1 parent 034c55e commit 7710ea9

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

components/mdns/mdns.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,10 +2738,17 @@ static bool _hostname_is_ours(const char * hostname)
27382738
return false;
27392739
}
27402740

2741+
/**
2742+
* @brief Adds a delegated hostname to the linked list
2743+
* @param hostname Host name pointer
2744+
* @param address_list Address list
2745+
* @return true on success
2746+
* false if the host wasn't attached (this is our hostname, or alloc failure) so we have to free the structs
2747+
*/
27412748
static bool _mdns_delegate_hostname_add(const char * hostname, mdns_ip_addr_t * address_list)
27422749
{
27432750
if (_hostname_is_ours(hostname)) {
2744-
return true;
2751+
return false;
27452752
}
27462753

27472754
mdns_host_item_t * host = (mdns_host_item_t *)malloc(sizeof(mdns_host_item_t));
@@ -2789,6 +2796,18 @@ static mdns_ip_addr_t * copy_address_list(const mdns_ip_addr_t * address_list)
27892796
return head;
27902797
}
27912798

2799+
static void free_delegated_hostnames(void)
2800+
{
2801+
mdns_host_item_t * host = _mdns_host_list;
2802+
while (host != NULL) {
2803+
free_address_list(host->address_list);
2804+
free((char *)host->hostname);
2805+
mdns_host_item_t *item = host;
2806+
host = host->next;
2807+
free(item);
2808+
}
2809+
}
2810+
27922811
static bool _mdns_delegate_hostname_remove(const char * hostname)
27932812
{
27942813
mdns_srv_item_t * srv = _mdns_server->services;
@@ -4657,8 +4676,11 @@ static void _mdns_execute_action(mdns_action_t * action)
46574676
_mdns_packet_free(action->data.rx_handle.packet);
46584677
break;
46594678
case ACTION_DELEGATE_HOSTNAME_ADD:
4660-
_mdns_delegate_hostname_add(action->data.delegate_hostname.hostname,
4661-
action->data.delegate_hostname.address_list);
4679+
if (!_mdns_delegate_hostname_add(action->data.delegate_hostname.hostname,
4680+
action->data.delegate_hostname.address_list)) {
4681+
free((char *)action->data.delegate_hostname.hostname);
4682+
free_address_list(action->data.delegate_hostname.address_list);
4683+
}
46624684
break;
46634685
case ACTION_DELEGATE_HOSTNAME_REMOVE:
46644686
_mdns_delegate_hostname_remove(action->data.delegate_hostname.hostname);
@@ -5008,6 +5030,7 @@ void mdns_free(void)
50085030
#endif
50095031

50105032
mdns_service_remove_all();
5033+
free_delegated_hostnames();
50115034
_mdns_service_task_stop();
50125035
for (i=0; i<MDNS_IF_MAX; i++) {
50135036
for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {

0 commit comments

Comments
 (0)