linux-sctp.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] sctp: replace usage of found with dedicated list iterator variable
@ 2022-03-24  7:22 Jakob Koschel
  2022-03-25 20:57 ` Marcelo Ricardo Leitner
  0 siblings, 1 reply; 2+ messages in thread
From: Jakob Koschel @ 2022-03-24  7:22 UTC (permalink / raw)
  To: Vlad Yasevich
  Cc: Neil Horman, Marcelo Ricardo Leitner, David S. Miller,
	Jakub Kicinski, Paolo Abeni, linux-sctp, netdev, linux-kernel,
	Mike Rapoport, Brian Johannesmeyer, Cristiano Giuffrida, Bos,
	H.J.,
	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].

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.

Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/
Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
---
 net/sctp/bind_addr.c | 15 +++++++--------
 net/sctp/ipv6.c      | 24 +++++++++++-------------
 net/sctp/protocol.c  | 24 +++++++++++-------------
 3 files changed, 29 insertions(+), 34 deletions(-)

diff --git a/net/sctp/bind_addr.c b/net/sctp/bind_addr.c
index 59e653b528b1..c85ade7863bf 100644
--- a/net/sctp/bind_addr.c
+++ b/net/sctp/bind_addr.c
@@ -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;
diff --git a/net/sctp/ipv6.c b/net/sctp/ipv6.c
index 470dbdc27d58..803950277f56 100644
--- a/net/sctp/ipv6.c
+++ b/net/sctp/ipv6.c
@@ -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;
 	}
diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
index 35928fefae33..6f1d7fd83465 100644
--- a/net/sctp/protocol.c
+++ b/net/sctp/protocol.c
@@ -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
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] sctp: replace usage of found with dedicated list iterator variable
  2022-03-24  7:22 [PATCH] sctp: replace usage of found with dedicated list iterator variable Jakob Koschel
@ 2022-03-25 20:57 ` Marcelo Ricardo Leitner
  0 siblings, 0 replies; 2+ messages in thread
From: Marcelo Ricardo Leitner @ 2022-03-25 20:57 UTC (permalink / raw)
  To: Jakob Koschel
  Cc: Vlad Yasevich, Neil Horman, David S. Miller, Jakub Kicinski,
	Paolo Abeni, linux-sctp, netdev, linux-kernel, Mike Rapoport,
	Brian Johannesmeyer, Cristiano Giuffrida, Bos, H.J.

On Thu, Mar 24, 2022 at 08:22:57AM +0100, Jakob Koschel wrote:
> 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.
> 
> Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/
> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>

Acked-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-03-25 20:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24  7:22 [PATCH] sctp: replace usage of found with dedicated list iterator variable Jakob Koschel
2022-03-25 20:57 ` Marcelo Ricardo Leitner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).