Skip to content

Commit a57be7b

Browse files
committed
mdns: Fix null-service issue when parsing packets
Closes #8307
1 parent 2c10071 commit a57be7b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Diff for: components/mdns/mdns.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ static char * _mdns_mangle_name(char* in) {
158158
static bool _mdns_service_match(const mdns_service_t * srv, const char * service, const char * proto,
159159
const char * hostname)
160160
{
161+
if (!service || !proto) {
162+
return false;
163+
}
161164
return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) &&
162165
(_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
163166
}
@@ -289,6 +292,12 @@ static bool _mdns_instance_name_match(const char *lhs, const char *rhs)
289292
static bool _mdns_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service,
290293
const char *proto, const char *hostname)
291294
{
295+
// service and proto must be supplied, if not this instance won't match
296+
if (!service || !proto) {
297+
return false;
298+
}
299+
// instance==NULL -> _mdns_instance_name_match() will check the default instance
300+
// hostname==NULL -> matches if instance, service and proto matches
292301
return !strcasecmp(srv->service, service) && _mdns_instance_name_match(srv->instance, instance) &&
293302
!strcasecmp(srv->proto, proto) && (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
294303
}
@@ -1610,7 +1619,7 @@ static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_
16101619
shared = q->type == MDNS_TYPE_PTR || q->type == MDNS_TYPE_SDPTR || !parsed_packet->probe;
16111620
if (q->type == MDNS_TYPE_SRV || q->type == MDNS_TYPE_TXT) {
16121621
mdns_srv_item_t *service = _mdns_get_service_item_instance(q->host, q->service, q->proto, NULL);
1613-
if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
1622+
if (service == NULL || !_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
16141623
_mdns_free_tx_packet(packet);
16151624
return;
16161625
}
@@ -3382,8 +3391,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
33823391
_mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
33833392
packet->tcpip_if, packet->ip_protocol, ttl);
33843393
} else if ((discovery || ours) && !name->sub && _mdns_name_is_ours(name)) {
3385-
if (discovery) {
3386-
service = _mdns_get_service_item(name->service, name->proto, NULL);
3394+
if (discovery && (service = _mdns_get_service_item(name->service, name->proto, NULL))) {
33873395
_mdns_remove_parsed_question(parsed_packet, MDNS_TYPE_SDPTR, service);
33883396
} else if (service && parsed_packet->questions && !parsed_packet->probe) {
33893397
_mdns_remove_parsed_question(parsed_packet, type, service);

0 commit comments

Comments
 (0)