You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by rl...@apache.org on 2019/07/28 11:00:51 UTC

[celix] 03/03: Removed memleak from pubsub admins

This is an automated email from the ASF dual-hosted git repository.

rlenferink pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 4e003c817f15bd5f86ae55de9f0867932f36ff1f
Author: Roy Lenferink <le...@gmail.com>
AuthorDate: Sun Jul 28 12:28:49 2019 +0200

    Removed memleak from pubsub admins
---
 .../pubsub_admin_nanomsg/src/pubsub_nanomsg_admin.cc   | 13 +++----------
 bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_admin.c | 13 +++----------
 .../pubsub_admin_udp_mc/src/pubsub_udpmc_admin.c       | 13 +++----------
 bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c | 13 +++----------
 libs/utils/include/ip_utils.h                          |  2 +-
 libs/utils/src/ip_utils.c                              | 18 ++++++++----------
 6 files changed, 21 insertions(+), 51 deletions(-)

diff --git a/bundles/pubsub/pubsub_admin_nanomsg/src/pubsub_nanomsg_admin.cc b/bundles/pubsub/pubsub_admin_nanomsg/src/pubsub_nanomsg_admin.cc
index 5245d03..f9549ee 100644
--- a/bundles/pubsub/pubsub_admin_nanomsg/src/pubsub_nanomsg_admin.cc
+++ b/bundles/pubsub/pubsub_admin_nanomsg/src/pubsub_nanomsg_admin.cc
@@ -48,17 +48,10 @@ pubsub_nanomsg_admin::pubsub_nanomsg_admin(celix_bundle_context_t *_ctx):
     if (confIp != NULL) {
         if (strchr(confIp, '/') != NULL) {
             // IP with subnet prefix specified
-            char *found_if_ip = calloc(16, sizeof(char));
-            celix_status_t ip_status = ipUtils_findIpBySubnet(confIp, &found_if_ip);
-            if (ip_status == CELIX_SUCCESS) {
-                if (found_if_ip != NULL)
-                    ip = strndup(found_if_ip, 16);
-                else
-                    L_WARN("[PSA_NANOMSG] Could not find interface for requested subnet %s", confIp);
-            } else {
-                L_ERROR("[PSA_NANOMSG] Error while searching for available network interface for subnet %s", confIp);
+            ip = ipUtils_findIpBySubnet(confIp);
+            if (ip == NULL) {
+                L_WARN("[PSA_NANOMSG] Could not find interface for requested subnet %s", confIp);
             }
-            free(found_if_ip);
         } else {
             // IP address specified
             ip = strndup(confIp, 1024);
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_admin.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_admin.c
index 473d051..3711a98 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_admin.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_admin.c
@@ -105,17 +105,10 @@ pubsub_tcp_admin_t* pubsub_tcpAdmin_create(celix_bundle_context_t *ctx, log_help
     if (confIp != NULL) {
         if (strchr(confIp, '/') != NULL) {
             // IP with subnet prefix specified
-            char *found_if_ip = calloc(16, sizeof(char));
-            celix_status_t ip_status = ipUtils_findIpBySubnet(confIp, &found_if_ip);
-            if (ip_status == CELIX_SUCCESS) {
-                if (found_if_ip != NULL)
-                    ip = strndup(found_if_ip, 16);
-                else
-                    L_WARN("[PSA_TCP] Could not find interface for requested subnet %s", confIp);
-            } else {
-                L_ERROR("[PSA_TCP] Error while searching for available network interface for subnet %s", confIp);
+            ip = ipUtils_findIpBySubnet(confIp);
+            if (ip == NULL) {
+                L_WARN("[PSA_TCP] Could not find interface for requested subnet %s", confIp);
             }
-            free(found_if_ip);
         } else {
             // IP address specified
             ip = strndup(confIp, 1024);
diff --git a/bundles/pubsub/pubsub_admin_udp_mc/src/pubsub_udpmc_admin.c b/bundles/pubsub/pubsub_admin_udp_mc/src/pubsub_udpmc_admin.c
index 770ad23..990594f 100644
--- a/bundles/pubsub/pubsub_admin_udp_mc/src/pubsub_udpmc_admin.c
+++ b/bundles/pubsub/pubsub_admin_udp_mc/src/pubsub_udpmc_admin.c
@@ -105,17 +105,10 @@ pubsub_udpmc_admin_t* pubsub_udpmcAdmin_create(celix_bundle_context_t *ctx, log_
     if (mcIpProp != NULL) {
         if (strchr(mcIpProp, '/') != NULL) {
             // IP with subnet prefix specified
-            char *found_if_ip = calloc(16, sizeof(char));
-            celix_status_t ip_status = ipUtils_findIpBySubnet(mcIpProp, &found_if_ip);
-            if (ip_status == CELIX_SUCCESS) {
-                if (found_if_ip != NULL)
-                    if_ip = strndup(found_if_ip, 16);
-                else
-                    L_WARN("[PSA_UDPMC] Could not find interface for requested subnet %s", mcIpProp);
-            } else {
-                L_ERROR("[PSA_UDPMC] Error while searching for available network interface for subnet %s", mcIpProp);
+            if_ip = ipUtils_findIpBySubnet(mcIpProp);
+            if (if_ip == NULL) {
+                L_WARN("[PSA_UDPMC] Could not find interface for requested subnet %s", mcIpProp);
             }
-            free(found_if_ip);
         } else {
             // IP address specified
             mc_ip = strndup(mcIpProp, 1024);
diff --git a/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c b/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c
index 8fa14ad..8335287 100644
--- a/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c
+++ b/bundles/pubsub/pubsub_admin_zmq/src/pubsub_zmq_admin.c
@@ -105,17 +105,10 @@ pubsub_zmq_admin_t* pubsub_zmqAdmin_create(celix_bundle_context_t *ctx, log_help
     if (confIp != NULL) {
         if (strchr(confIp, '/') != NULL) {
             // IP with subnet prefix specified
-            char *found_if_ip = calloc(16, sizeof(char));
-            celix_status_t ip_status = ipUtils_findIpBySubnet(confIp, &found_if_ip);
-            if (ip_status == CELIX_SUCCESS) {
-                if (found_if_ip != NULL)
-                    ip = strndup(found_if_ip, 16);
-                else
-                    L_WARN("[PSA_ZMQ] Could not find interface for requested subnet %s", confIp);
-            } else {
-                L_ERROR("[PSA_ZMQ] Error while searching for available network interface for subnet %s", confIp);
+            ip = ipUtils_findIpBySubnet(confIp);
+            if (ip == NULL) {
+                L_WARN("[PSA_ZMQ] Could not find interface for requested subnet %s", confIp);
             }
-            free(found_if_ip);
         } else {
             // IP address specified
             ip = strndup(confIp, 1024);
diff --git a/libs/utils/include/ip_utils.h b/libs/utils/include/ip_utils.h
index 8f8fe5c..b95694a 100644
--- a/libs/utils/include/ip_utils.h
+++ b/libs/utils/include/ip_utils.h
@@ -44,7 +44,7 @@ UTILS_EXPORT unsigned int ipUtils_prefixToBitmask(unsigned int prefix);
 
 UTILS_EXPORT int ipUtils_netmaskToPrefix(const char *netmask);
 
-UTILS_EXPORT celix_status_t ipUtils_findIpBySubnet(const char *ipWithPrefix, char **ip);
+UTILS_EXPORT char *ipUtils_findIpBySubnet(const char *ipWithPrefix);
 
 #ifdef __cplusplus
 }
diff --git a/libs/utils/src/ip_utils.c b/libs/utils/src/ip_utils.c
index 31c428a..8d4f0d3 100644
--- a/libs/utils/src/ip_utils.c
+++ b/libs/utils/src/ip_utils.c
@@ -97,14 +97,13 @@ int ipUtils_netmaskToPrefix(const char *netmask) {
 
 /** Finds an IP of the available network interfaces of the machine by specifying an CIDR subnet.
  *
- * @param ipWithPrefix  [in] IP with prefix, e.g. 192.168.1.0/24
- * @param ip            [out] In case a matching interface could be found, an allocated string containing the IP of the
- *                      interface will be set, e.g. 192.168.1.16. Memory for the new string can be freed with free().
- *                      When no matching interface is found NULL will be set.
- * @return              CELIX_SUCCESS for success. Otherwise an error status from celix_errno.h will be set.
+ * @param ipWithPrefix  IP with prefix, e.g. 192.168.1.0/24
+ * @return ip           In case a matching interface could be found, an allocated string containing the IP of the
+ *                      interface will be returned, e.g. 192.168.1.16. Memory for the new string can be freed with free().
+ *                      When no matching interface is found NULL will be returned.
  */
-celix_status_t ipUtils_findIpBySubnet(const char *ipWithPrefix, char **ip) {
-    celix_status_t status = CELIX_SUCCESS;
+char *ipUtils_findIpBySubnet(const char *ipWithPrefix) {
+    char *ip = NULL;
 
     char *input = strndup(ipWithPrefix, 19); // Make a copy as otherwise strtok_r manipulates the input string
 
@@ -147,17 +146,16 @@ celix_status_t ipUtils_findIpBySubnet(const char *ipWithPrefix, char **ip) {
         unsigned int ifIpAsUint = ipUtils_ipToUnsignedInt(if_addr);
         int ifPrefix = ipUtils_netmaskToPrefix(if_netmask);
         if (ifPrefix == -1) {
-            status = CELIX_ILLEGAL_ARGUMENT;
             break;
         }
 
         if (ifIpAsUint >= ipRangeStart && ifIpAsUint <= ipRangeStop && inputPrefix >= ifPrefix) {
-            *ip = strndup(if_addr, 1024);
+            ip = strndup(if_addr, 1024);
             break;
         }
     }
     freeifaddrs(ifap);
     free(input);
 
-    return status;
+    return ip;
 }