From 4bb89900f7773b63d1069a822942e3020b66db71 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Sun, 31 Dec 2023 01:28:11 +0100 Subject: [PATCH] Fix memleak in celix_utils_findIpInSubnet when calling getifaddrs --- libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc | 10 +++------- libs/utils/src/ip_utils.c | 2 ++ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc index 8bbed74aa..9f0dce5bf 100644 --- a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc +++ b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc @@ -19,8 +19,6 @@ #include -#include - #include "celix_ip_utils.h" #include "celix_err.h" @@ -56,25 +54,24 @@ TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToDuplicateStringTest) { char* ipAddr = nullptr; //first call to celix_utils_strdup fails - celix_ei_expect_celix_utils_strdup((void *) &celix_utils_findIpInSubnet, 0, nullptr); + celix_ei_expect_celix_utils_strdup((void*)celix_utils_findIpInSubnet, 0, nullptr); auto status = celix_utils_findIpInSubnet("192.168.1.0/24", &ipAddr); EXPECT_EQ(status, ENOMEM); EXPECT_EQ(ipAddr, nullptr); EXPECT_EQ(errCount++, celix_err_getErrorCount()); //second call to celix_utils_strdup fails (in ifa -> ifa_next loop) - celix_ei_expect_celix_utils_strdup((void *) &celix_utils_findIpInSubnet, 0, nullptr, 2); + celix_ei_expect_celix_utils_strdup((void*)celix_utils_findIpInSubnet, 0, nullptr, 2); status = celix_utils_findIpInSubnet("127.0.0.1/24", &ipAddr); EXPECT_EQ(status, ENOMEM); EXPECT_EQ(ipAddr, nullptr); EXPECT_EQ(errCount++, celix_err_getErrorCount()); - celix_ei_expect_celix_utils_strdup((void *) &celix_utils_convertIpToUint, 0, nullptr); + celix_ei_expect_celix_utils_strdup((void*)celix_utils_convertIpToUint, 0, nullptr); bool converted; auto ipAsUint = celix_utils_convertIpToUint("192.168.1.0", &converted); EXPECT_EQ(ipAsUint, 0); EXPECT_FALSE(converted); - EXPECT_EQ(errno, ENOMEM); EXPECT_EQ(errCount++, celix_err_getErrorCount()); } @@ -82,6 +79,5 @@ TEST_F(IpUtilsWithErrorInjectionTestSuite, FailToCalledTest) { celix_ei_expect_calloc((void*)celix_utils_convertUintToIp, 0, nullptr); auto ip = celix_utils_convertUintToIp(3232235840); EXPECT_EQ(ip, nullptr); - EXPECT_EQ(errno, ENOMEM); EXPECT_EQ(1, celix_err_getErrorCount()); } diff --git a/libs/utils/src/ip_utils.c b/libs/utils/src/ip_utils.c index 316382018..12cde0a38 100644 --- a/libs/utils/src/ip_utils.c +++ b/libs/utils/src/ip_utils.c @@ -209,6 +209,7 @@ celix_status_t celix_utils_findIpInSubnet(const char* subnetCidrNotation, char** if (ifIpAsUint >= ipRangeStart && ifIpAsUint <= ipRangeStop && inputPrefix >= ifPrefix) { char* ip = celix_utils_strdup(if_addr); if (!ip) { + freeifaddrs(ifap); celix_err_push("Failed to duplicate IP address"); return CELIX_ENOMEM; } @@ -216,5 +217,6 @@ celix_status_t celix_utils_findIpInSubnet(const char* subnetCidrNotation, char** break; } } + freeifaddrs(ifap); return CELIX_SUCCESS; }