From 269c4c74d29bacf7ba98bd0ca765cc34d9f37105 Mon Sep 17 00:00:00 2001 From: gfrodo Date: Mon, 10 Dec 2018 11:51:54 +0100 Subject: [PATCH] udp_multicast_example: better handling wrong addresses inet_aton returns 0 on failure, but socket_add_ipv4_multicast_group has to return negative values for failures getaddrinfo sets res to zero of address could not resolved, but doesn't necessarily return an error. res is now checked for zero before dereferencing Merges https://github.com/espressif/esp-idf/pull/2814 --- .../sockets/udp_multicast/main/udp_multicast_example_main.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c b/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c index e3bb74284..03025125b 100644 --- a/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c +++ b/examples/protocols/sockets/udp_multicast/main/udp_multicast_example_main.c @@ -74,6 +74,8 @@ static int socket_add_ipv4_multicast_group(int sock, bool assign_source_if) err = inet_aton(MULTICAST_IPV4_ADDR, &imreq.imr_multiaddr.s_addr); if (err != 1) { ESP_LOGE(V4TAG, "Configured IPV4 multicast address '%s' is invalid.", MULTICAST_IPV4_ADDR); + // Errors in the return value have to be negative + err = -1; goto err; } ESP_LOGI(TAG, "Configured IPV4 Multicast address %s", inet_ntoa(imreq.imr_multiaddr.s_addr)); @@ -426,6 +428,10 @@ static void mcast_example_task(void *pvParameters) ESP_LOGE(TAG, "getaddrinfo() failed for IPV4 destination address. error: %d", err); break; } + if (res == 0) { + ESP_LOGE(TAG, "getaddrinfo() did not return any addresses"); + break; + } #ifdef CONFIG_EXAMPLE_IPV4_ONLY ((struct sockaddr_in *)res->ai_addr)->sin_port = htons(UDP_PORT); inet_ntoa_r(((struct sockaddr_in *)res->ai_addr)->sin_addr, addrbuf, sizeof(addrbuf)-1);