Skip to content

Commit

Permalink
Fix memleak in celix_utils_findIpInSubnet when calling getifaddrs
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Dec 31, 2023
1 parent f2317ed commit 4bb8990
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 3 additions & 7 deletions libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

#include <gtest/gtest.h>

#include <errno.h>

#include "celix_ip_utils.h"
#include "celix_err.h"

Expand Down Expand Up @@ -56,32 +54,30 @@ 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());
}

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());
}
2 changes: 2 additions & 0 deletions libs/utils/src/ip_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ 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;
}
*foundIp = ip;
break;
}
}
freeifaddrs(ifap);
return CELIX_SUCCESS;
}

0 comments on commit 4bb8990

Please sign in to comment.