diff mbox series

[net-next,6/8] ipv6: mcast: use min() to simplify the code

Message ID 20240822133908.1042240-7-lizetao1@huawei.com (mailing list archive)
State Accepted
Commit 26549dab8a4676ce549cb20bf384daf458a9ea24
Delegated to: Netdev Maintainers
Headers show
Series Some modifications to optimize code readability | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 16 this patch: 16
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 16 this patch: 16
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 17 this patch: 17
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 17 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-08-25--21-00 (tests: 714)

Commit Message

lizetao Aug. 22, 2024, 1:39 p.m. UTC
When coping sockaddr in ip6_mc_msfget(), the time of copies
depends on the minimum value between sl_count and gf_numsrc.
Using min() here is very semantic.

Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
 net/ipv6/mcast.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Simon Horman Aug. 24, 2024, 5:57 p.m. UTC | #1
On Thu, Aug 22, 2024 at 09:39:06PM +0800, Li Zetao wrote:
> When coping sockaddr in ip6_mc_msfget(), the time of copies
> depends on the minimum value between sl_count and gf_numsrc.
> Using min() here is very semantic.
> 
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> ---
>  net/ipv6/mcast.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
> index 7ba01d8cfbae..b244dbf61d5f 100644
> --- a/net/ipv6/mcast.c
> +++ b/net/ipv6/mcast.c
> @@ -586,7 +586,8 @@ int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
>  	const struct in6_addr *group;
>  	struct ipv6_mc_socklist *pmc;
>  	struct ip6_sf_socklist *psl;
> -	int i, count, copycount;
> +	unsigned int count;
> +	int i, copycount;
>  
>  	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
>  
> @@ -610,7 +611,7 @@ int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
>  	psl = sock_dereference(pmc->sflist, sk);
>  	count = psl ? psl->sl_count : 0;

Both count and psl->sl_count are unsigned int,
so this looks safe (and more correct than what it replaces, IMHO).

>  
> -	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
> +	copycount = min(count, gsf->gf_numsrc);

And gsf->gf_numsrc is a __u32, so min operating on it and
count looks safe to me.

Further, the code it replaces seems to be a max() operation in
both intent and function.

Reviewed-by: Simon Horman <horms@kernel.org>

>  	gsf->gf_numsrc = count;
>  	for (i = 0; i < copycount; i++) {
>  		struct sockaddr_in6 *psin6;
> -- 
> 2.34.1
> 
>
diff mbox series

Patch

diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
index 7ba01d8cfbae..b244dbf61d5f 100644
--- a/net/ipv6/mcast.c
+++ b/net/ipv6/mcast.c
@@ -586,7 +586,8 @@  int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
 	const struct in6_addr *group;
 	struct ipv6_mc_socklist *pmc;
 	struct ip6_sf_socklist *psl;
-	int i, count, copycount;
+	unsigned int count;
+	int i, copycount;
 
 	group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr;
 
@@ -610,7 +611,7 @@  int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
 	psl = sock_dereference(pmc->sflist, sk);
 	count = psl ? psl->sl_count : 0;
 
-	copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
+	copycount = min(count, gsf->gf_numsrc);
 	gsf->gf_numsrc = count;
 	for (i = 0; i < copycount; i++) {
 		struct sockaddr_in6 *psin6;