Fixed memory leak
[platform/core/connectivity/net-config.git] / src / network-state.c
index bbd34a4..1c08078 100755 (executable)
@@ -560,7 +560,8 @@ static char *__netconfig_get_preferred_ipv6_address(char *profile)
                        while (g_variant_iter_loop(sub_iter, "{sv}", &sub_key, &variant)) {
                                if (g_strcmp0(sub_key, "Address") == 0) {
                                        value = g_variant_get_string(variant, NULL);
-                                       preferred_address6 = g_strdup(value);
+                                       if (!preferred_address6)
+                                               preferred_address6 = g_strdup(value);
                                }
                        }
                        g_variant_iter_free(sub_iter);
@@ -1256,7 +1257,8 @@ char *netconfig_get_ifname(const char *profile)
                        while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
                                if (g_strcmp0(key1, "Interface") == 0) {
                                        value = g_variant_get_string(variant, NULL);
-                                       ifname = g_strdup(value);
+                                       if (!ifname)
+                                               ifname = g_strdup(value);
                                        g_free(key1);
                                        g_variant_unref(variant);
                                        break;
@@ -1293,10 +1295,6 @@ static gboolean handle_add_route(
        gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr, gw_str,
                "netmask", netmask, "dev", interface, NULL };
        gchar *const envs[] = { NULL };
-       const gchar* buf = NULL;
-       gchar* ch = NULL;
-       int prefix_len = 0;
-       int pos = 0;
 
        DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
 
@@ -1322,17 +1320,7 @@ static gboolean handle_add_route(
                        return TRUE;
                }
 
-               buf = ip_addr;
-               ch = strchr(buf, '/');
-               pos = ch - buf + 1;
-               if (ch) {
-                       prefix_len = atoi(ch + 1);
-                       ip_addr[pos-1] = '\0';
-               } else {
-                       prefix_len = 128;
-               }
-
-               if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
+               if (netconfig_add_route_ipv6(interface, gateway) < 0) {
                        DBG("Failed to add a new route");
                        netconfig_error_permission_denied(context);
                        return TRUE;
@@ -1363,10 +1351,6 @@ static gboolean handle_remove_route(
        gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr, gw_str,
                "netmask", netmask, "dev", interface, NULL };
        char *const envs[] = { NULL };
-       const char* buf = NULL;
-       char* ch = NULL;
-       int prefix_len = 0;
-       int pos = 0;
 
        DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
 
@@ -1390,17 +1374,7 @@ static gboolean handle_remove_route(
                        return TRUE;
                }
 
-               buf = ip_addr;
-               ch = strchr(buf, '/');
-               pos = ch - buf + 1;
-               if (ch) {
-                       prefix_len = atoi(ch + 1);
-                       ip_addr[pos-1] = '\0';
-               } else {
-                       prefix_len = 128;
-               }
-
-               if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
+               if (netconfig_del_route_ipv6(interface, gateway) < 0) {
                        DBG("Failed to remove the route");
                        netconfig_error_permission_denied(context);
                        return TRUE;