Skip to contentSkip to conversation

sctp: replace usage of found with dedicated list iterator variable

PATCH In review Jakob Koschel 2 participants 2 messages Mar 24, 2022 – Mar 25, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sctp: replace usage of found with dedicated list iterator variable

To move the list iterator variable into the list_for_each_entry_*() macro in the future it should be avoided to use the list iterator variable after the loop body.

To *never* use the list iterator variable after the loop it was concluded to use a separate iterator variable instead of a found boolean [1].

This removes the need to use a found variable and simply checking if the variable was set, can determine if the break/goto was hit.

---
 net/sctp/bind_addr.c | 15 +++++++--------
 net/sctp/ipv6.c      | 24 +++++++++++-------------
 net/sctp/protocol.c  | 24 +++++++++++-------------
 3 files changed, 29 insertions(+), 34 deletions(-)
net/sctp/bind_addr.c +7 -8
index 59e653b528b1..c85ade7863bf 100644
@@ -172,23 +172,22 @@ int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
  */
 int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr)
 {
-	struct sctp_sockaddr_entry *addr, *temp;
-	int found = 0;
+	struct sctp_sockaddr_entry *addr = NULL, *iter, *temp;
 
 	/* We hold the socket lock when calling this function,
 	 * and that acts as a writer synchronizing lock.
 	 */
-	list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
-		if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
+	list_for_each_entry_safe(iter, temp, &bp->address_list, list) {
+		if (sctp_cmp_addr_exact(&iter->a, del_addr)) {
 			/* Found the exact match. */
-			found = 1;
-			addr->valid = 0;
-			list_del_rcu(&addr->list);
+			addr = iter;
+			iter->valid = 0;
+			list_del_rcu(&iter->list);
 			break;
 		}
 	}
 
-	if (found) {
+	if (addr) {
 		kfree_rcu(addr, rcu);
 		SCTP_DBG_OBJCNT_DEC(addr);
 		return 0;
net/sctp/ipv6.c +11 -13
index 470dbdc27d58..803950277f56 100644
@@ -76,10 +76,8 @@ static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
 				void *ptr)
 {
 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
-	struct sctp_sockaddr_entry *addr = NULL;
-	struct sctp_sockaddr_entry *temp;
+	struct sctp_sockaddr_entry *addr = NULL, *iter, *temp;
 	struct net *net = dev_net(ifa->idev->dev);
-	int found = 0;
 
 	switch (ev) {
 	case NETDEV_UP:
@@ -97,21 +95,21 @@ static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
 		break;
 	case NETDEV_DOWN:
 		spin_lock_bh(&net->sctp.local_addr_lock);
-		list_for_each_entry_safe(addr, temp,
-					&net->sctp.local_addr_list, list) {
-			if (addr->a.sa.sa_family == AF_INET6 &&
-			    ipv6_addr_equal(&addr->a.v6.sin6_addr,
+		list_for_each_entry_safe(iter, temp,
+					 &net->sctp.local_addr_list, list) {
+			if (iter->a.sa.sa_family == AF_INET6 &&
+			    ipv6_addr_equal(&iter->a.v6.sin6_addr,
 					    &ifa->addr) &&
-			    addr->a.v6.sin6_scope_id == ifa->idev->dev->ifindex) {
-				sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
-				found = 1;
-				addr->valid = 0;
-				list_del_rcu(&addr->list);
+			    iter->a.v6.sin6_scope_id == ifa->idev->dev->ifindex) {
+				sctp_addr_wq_mgmt(net, iter, SCTP_ADDR_DEL);
+				addr = iter;
+				iter->valid = 0;
+				list_del_rcu(&iter->list);
 				break;
 			}
 		}
 		spin_unlock_bh(&net->sctp.local_addr_lock);
-		if (found)
+		if (addr)
 			kfree_rcu(addr, rcu);
 		break;
 	}
net/sctp/protocol.c +11 -13
index 35928fefae33..6f1d7fd83465 100644
@@ -777,10 +777,8 @@ static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
 			       void *ptr)
 {
 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
-	struct sctp_sockaddr_entry *addr = NULL;
-	struct sctp_sockaddr_entry *temp;
+	struct sctp_sockaddr_entry *addr = NULL, *iter, *temp;
 	struct net *net = dev_net(ifa->ifa_dev->dev);
-	int found = 0;
 
 	switch (ev) {
 	case NETDEV_UP:
@@ -797,20 +795,20 @@ static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
 		break;
 	case NETDEV_DOWN:
 		spin_lock_bh(&net->sctp.local_addr_lock);
-		list_for_each_entry_safe(addr, temp,
-					&net->sctp.local_addr_list, list) {
-			if (addr->a.sa.sa_family == AF_INET &&
-					addr->a.v4.sin_addr.s_addr ==
-					ifa->ifa_local) {
-				sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
-				found = 1;
-				addr->valid = 0;
-				list_del_rcu(&addr->list);
+		list_for_each_entry_safe(iter, temp,
+					 &net->sctp.local_addr_list, list) {
+			if (iter->a.sa.sa_family == AF_INET &&
+			    iter->a.v4.sin_addr.s_addr ==
+			    ifa->ifa_local) {
+				sctp_addr_wq_mgmt(net, iter, SCTP_ADDR_DEL);
+				addr = iter;
+				iter->valid = 0;
+				list_del_rcu(&iter->list);
 				break;
 			}
 		}
 		spin_unlock_bh(&net->sctp.local_addr_lock);
-		if (found)
+		if (addr)
 			kfree_rcu(addr, rcu);
 		break;
 	}

base-commit: f443e374ae131c168a065ea1748feac6b2e76613

Signature · 1 line
2.25.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jakob Koschel

To move the list iterator variable into the list_for_each_entry_*() macro in the future it should be avoided to use the list iterator variable after the loop body.

To *never* use the list iterator variable after the loop it was concluded to use a separate iterator variable instead of a found boolean [1].

Quoted 5 lines

This removes the need to use a found variable and simply checking if the variable was set, can determine if the break/goto was hit.