Skip to contentSkip to conversation

connection: Remove Unnecessary '__connman_service_get_index' Calls

PATCH New Grant Erickson 1 participant 3 messages Nov 29, 2023

lore
Share & tools
lore
#0 lore raw

cover connection: Remove Unnecessary '__connman_service_get_index' Calls

This removed unnecessary calls to '__connman_service_get_index' that are either unused or are redundant.

Grant Erickson (2):
connection: Remove unused local variables.
connection: Remove unnecessary '__connman_service_get_index' calls.

 src/connection.c | 32 ++++++++------------------------
 1 file changed, 8 insertions(+), 24 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH 1/2 connection: Remove unused local variables.

In __connman_connection_gateway_remove, both 'index' and 'interface' are set; however, neither are used. Remove these unused local variables.

---
 src/connection.c | 10 ----------
 1 file changed, 10 deletions(-)
src/connection.c +0 -10
index 297af227a9c3..5a98b8d57c84 100644
@@ -1595,22 +1595,12 @@ void __connman_connection_gateway_remove(struct connman_service *service,
 	struct gateway_data *data = NULL;
 	bool set_default4 = false, set_default6 = false;
         bool do_ipv4 = false, do_ipv6 = false;
-	int index;
-	g_autofree char *interface = NULL;
 	int err;
 
 	DBG("service %p (%s) type %d (%s)",
 		service, maybe_null(connman_service_get_identifier(service)),
 		type, __connman_ipconfig_type2string(type));
 
-	index = __connman_service_get_index(service);
-	if (index < 0)
-		return;
-
-	interface = connman_inet_ifname(index);
-
-	DBG("index %d (%s)", index, maybe_null(interface));
-
 	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
 		do_ipv4 = true;
 	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
Signature · 1 line
2.42.0
#2 lore raw

PATCH 2/2 connection: Remove unnecessary '__connman_service_get_index' calls.

In reply to Grant Erickson

In both 'set_default_gateway' and 'unset_default_gateway' there are unnecessary, redundant, and, per the below, circular calls to '__connman_service_get_index' to get the network interface index from the 'service' field of the gateway 'data' parameter passed to these functions.

However, in 'add_gateway', the gateway data 'index' field is assigned as follows:

    temp_data->index = index;

where 'index' comes from a parameter passed to 'add_gateway', from '__connman_connection_gateway_add':

    index = __connman_service_get_index(service);

that was sourced from the network service from which a strong reference was retained when the gateway data was initialized.

Consequently, the network interface index sought may simply be accessed from the 'index' field of the gateway data as:

    data->index
---
 src/connection.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
src/connection.c +8 -14
index 5a98b8d57c84..91ce39adc167 100644
@@ -859,7 +859,6 @@ static int add_gateway(struct connman_service *service,
 static void set_default_gateway(struct gateway_data *data,
 				enum connman_ipconfig_type type)
 {
-	int index;
 	int status4 = 0, status6 = 0;
 	bool do_ipv4 = false, do_ipv6 = false;
 
@@ -905,11 +904,9 @@ static void set_default_gateway(struct gateway_data *data,
 		return;
 	}
 
-	index = __connman_service_get_index(data->service);
-
 	if (do_ipv4 && data->ipv4_config &&
 			is_ipv4_addr_any_str(data->ipv4_config->gateway)) {
-		if (connman_inet_set_gateway_interface(index) < 0)
+		if (connman_inet_set_gateway_interface(data->index) < 0)
 			return;
 		data->ipv4_config->active = true;
 		goto done;
@@ -917,7 +914,7 @@ static void set_default_gateway(struct gateway_data *data,
 
 	if (do_ipv6 && data->ipv6_config &&
 			is_ipv6_addr_any_str(data->ipv6_config->gateway)) {
-		if (connman_inet_set_ipv6_gateway_interface(index) < 0)
+		if (connman_inet_set_ipv6_gateway_interface(data->index) < 0)
 			return;
 		data->ipv6_config->active = true;
 		goto done;
@@ -925,11 +922,11 @@ static void set_default_gateway(struct gateway_data *data,
 
 	if (do_ipv6 && data->ipv6_config)
 		status6 = __connman_inet_add_default_to_table(RT_TABLE_MAIN,
-					index, data->ipv6_config->gateway);
+					data->index, data->ipv6_config->gateway);
 
 	if (do_ipv4 && data->ipv4_config)
 		status4 = __connman_inet_add_default_to_table(RT_TABLE_MAIN,
-					index, data->ipv4_config->gateway);
+					data->index, data->ipv4_config->gateway);
 
 	if (status4 < 0 || status6 < 0)
 		return;
@@ -941,7 +938,6 @@ done:
 static void unset_default_gateway(struct gateway_data *data,
 				enum connman_ipconfig_type type)
 {
-	int index;
 	bool do_ipv4 = false, do_ipv6 = false;
 
 	DBG("data %p type %d (%s)", data,
@@ -985,28 +981,26 @@ static void unset_default_gateway(struct gateway_data *data,
 		return;
 	}
 
-	index = __connman_service_get_index(data->service);
-
 	if (do_ipv4 && data->ipv4_config &&
 			is_ipv4_addr_any_str(data->ipv4_config->gateway)) {
-		connman_inet_clear_gateway_interface(index);
+		connman_inet_clear_gateway_interface(data->index);
 		data->ipv4_config->active = false;
 		return;
 	}
 
 	if (do_ipv6 && data->ipv6_config &&
 			is_ipv6_addr_any_str(data->ipv6_config->gateway)) {
-		connman_inet_clear_ipv6_gateway_interface(index);
+		connman_inet_clear_ipv6_gateway_interface(data->index);
 		data->ipv6_config->active = false;
 		return;
 	}
 
 	if (do_ipv6 && data->ipv6_config)
-		connman_inet_clear_ipv6_gateway_address(index,
+		connman_inet_clear_ipv6_gateway_address(data->index,
 						data->ipv6_config->gateway);
 
 	if (do_ipv4 && data->ipv4_config)
-		connman_inet_clear_gateway_address(index,
+		connman_inet_clear_gateway_address(data->index,
 						data->ipv4_config->gateway);
 }
 
Signature · 1 line
2.42.0