From d4da9cb079cb13ff7aab6e19360962155882c101 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 19 Aug 2024 11:28:50 +0200 Subject: [PATCH] fix(mdns): Fix mdns mdns_lookup_service() to handle empty TXT the lookup_service API calls _copy_mdns_txt_items(), which tries to allocate new TXT records, but didn't handle the case with no TXT. Originally the _copy_mdns_txt_items() called calloc() with zero's which returned NULL (on espressif toolchain), so it's safe, but we could see an error message: E (1170) mdns: Cannot allocate memory (line: 6191, free heap: 281368 bytes) This commit addresses the empty TXT case and gets rid of the error message. --- components/mdns/mdns.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index cc359c803..d3961ab49 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -6185,6 +6185,10 @@ static mdns_txt_item_t *_copy_mdns_txt_items(mdns_txt_linked_item_t *items, uint ret_index++; } *txt_count = ret_index; + if (ret_index == 0) { // handle empty TXT + *txt_value_len = NULL; + return NULL; + } ret = (mdns_txt_item_t *)calloc(ret_index, sizeof(mdns_txt_item_t)); *txt_value_len = (uint8_t *)calloc(ret_index, sizeof(uint8_t)); if (!ret || !(*txt_value_len)) {