Skip to content

Commit 6cdf5ee

Browse files
mdns: Use memcpy() for copy to support non-text TXTs
* Original commit: espressif/esp-idf@6aefe9c
1 parent fcb5515 commit 6cdf5ee

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

components/mdns/mdns.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,37 @@ static inline uint8_t _mdns_append_string(uint8_t * packet, uint16_t * index, co
513513
return len + 1;
514514
}
515515

516+
/**
517+
* @brief appends one TXT record ("key=value" or "key")
518+
*
519+
* @param packet MDNS packet
520+
* @param index offset in the packet
521+
* @param txt one txt record
522+
*
523+
* @return length of added data: length of the added txt value + 1 on success
524+
* 0 if data won't fit the packet
525+
* -1 if invalid TXT entry
526+
*/
527+
static inline int append_one_txt_record_entry(uint8_t * packet, uint16_t * index, mdns_txt_linked_item_t * txt)
528+
{
529+
if (txt == NULL || txt->key == NULL) {
530+
return -1;
531+
}
532+
size_t key_len = strlen(txt->key);
533+
size_t len = key_len + txt->value_len + (txt->value ? 1 : 0);
534+
if ((*index + len + 1) >= MDNS_MAX_PACKET_SIZE) {
535+
return 0;
536+
}
537+
_mdns_append_u8(packet, index, len);
538+
memcpy(packet + *index, txt->key, key_len);
539+
if (txt->value) {
540+
packet[*index + key_len] = '=';
541+
memcpy(packet + *index + key_len + 1, txt->value, txt->value_len);
542+
}
543+
*index += len;
544+
return len + 1;
545+
}
546+
516547
/**
517548
* @brief appends FQDN to a packet, incrementing the index and
518549
* compressing the output if previous occurrence of the string (or part of it) has been found
@@ -775,19 +806,13 @@ static uint16_t _mdns_append_txt_record(uint8_t * packet, uint16_t * index, mdns
775806
uint16_t data_len_location = *index - 2;
776807
uint16_t data_len = 0;
777808

778-
char * tmp;
779809
mdns_txt_linked_item_t * txt = service->txt;
780810
while (txt) {
781-
if (asprintf(&tmp, "%s%s%.*s", txt->key, txt->value ? "=" : "", txt->value_len, txt->value) > 0) {
782-
uint8_t l = _mdns_append_string(packet, index, tmp);
783-
free(tmp);
784-
if (!l) {
785-
return 0;
786-
}
811+
int l = append_one_txt_record_entry(packet, index, txt);
812+
if (l > 0) {
787813
data_len += l;
788-
} else {
789-
HOOK_MALLOC_FAILED;
790-
// continue
814+
} else if (l == 0) { // TXT entry won't fit into the mdns packet
815+
return 0;
791816
}
792817
txt = txt->next;
793818
}
@@ -2574,17 +2599,10 @@ static int _mdns_check_txt_collision(mdns_service_t * service, const uint8_t * d
25742599

25752600
uint8_t ours[len];
25762601
uint16_t index = 0;
2577-
char * tmp;
25782602

25792603
txt = service->txt;
25802604
while (txt) {
2581-
if (asprintf(&tmp, "%s%s%.*s", txt->key, txt->value ? "=" : "", txt->value_len, txt->value) > 0) {
2582-
_mdns_append_string(ours, &index, tmp);
2583-
free(tmp);
2584-
} else {
2585-
HOOK_MALLOC_FAILED;
2586-
// continue
2587-
}
2605+
append_one_txt_record_entry(ours, &index, txt);
25882606
txt = txt->next;
25892607
}
25902608

0 commit comments

Comments
 (0)