From c3d38969cae89a8433aa286bbbb3960a42e99475 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Mon, 2 Dec 2019 13:09:35 +0900 Subject: [PATCH] Return a valid error code when adding an invalid IP address. Currently, passing an invalid IP address to interfaceAddAddress and friends results in an invalid error message: 12-02 13:00:42.675 interfaceAddAddress("testtap2355", "2001:db8::1/64", 64) -> ServiceSpecificException(-8, "InterfaceController error: Unknown error -8") <1.63ms> This is due to confusion between getaddrinfo error codes and errno values. Test: new unit test added to IpClientIntegrationTest Change-Id: Ifdaa4281a9bcf3998e3216472c5c1df0f5285214 --- libnetutils/ifc_utils.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c index 6af49bbdb..4e7340125 100644 --- a/libnetutils/ifc_utils.c +++ b/libnetutils/ifc_utils.c @@ -113,6 +113,10 @@ int string_to_ip(const char *string, struct sockaddr_storage *ss) { if (ret == 0) { memcpy(ss, ai->ai_addr, ai->ai_addrlen); freeaddrinfo(ai); + } else { + // Getaddrinfo has its own error codes. Convert to negative errno. + // There, the only thing that can reasonably happen is that the passed-in string is invalid. + ret = (ret == EAI_SYSTEM) ? -errno : -EINVAL; } return ret;