Re: [PATCH net-next] net/ipv6: silence 'passing zero to ERR_PTR()' warning

From: Eric Dumazet
Date: Fri Apr 07 2023 - 05:01:16 EST


On Fri, Apr 7, 2023 at 5:54 AM Haoyi Liu <iccccc@xxxxxxxxxxx> wrote:
>
> Smatch complains that if xfrm_lookup() returns NULL then this does a
> weird thing with "err":
>
> net/ ipv6/ icmp.c:411 icmpv6_route_lookup()
> warn: passing zero to ERR_PTR()
>
> Just return "dst2" directly instead of assigning it to"dst" and then
> looking up the value of "err". No functional change.
>
> Signed-off-by: Haoyi Liu <iccccc@xxxxxxxxxxx>
> Reviewed-by: Dongliang Mu <dzm91@xxxxxxxxxxx>
> ---
> The issue is found by static analysis, and the patch is remains untested.
> ---
> net/ipv6/icmp.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/ipv6/icmp.c b/net/ipv6/icmp.c
> index 1f53f2a74480..a5e77acead89 100644
> --- a/net/ipv6/icmp.c
> +++ b/net/ipv6/icmp.c
> @@ -395,7 +395,7 @@ static struct dst_entry *icmpv6_route_lookup(struct net *net,
> dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk, XFRM_LOOKUP_ICMP);
> if (!IS_ERR(dst2)) {
> dst_release(dst);
> - dst = dst2;
> + return dst2;
> } else {
> err = PTR_ERR(dst2);
> if (err == -EPERM) {
> --
> 2.25.1
>

Please cleanup this thing, this is a maze of returns, gotos, and
unnecessary 'else's

Thanks.

diff --git a/net/ipv6/icmp.c b/net/ipv6/icmp.c
index 1f53f2a74480c0b8433204b567e7f98ad1216ad6..c76861f1ff6e4ee12d2686c1d135b24595989dfa
100644
--- a/net/ipv6/icmp.c
+++ b/net/ipv6/icmp.c
@@ -395,14 +395,12 @@ static struct dst_entry
*icmpv6_route_lookup(struct net *net,
dst2 = xfrm_lookup(net, dst2, flowi6_to_flowi(&fl2), sk,
XFRM_LOOKUP_ICMP);
if (!IS_ERR(dst2)) {
dst_release(dst);
- dst = dst2;
- } else {
- err = PTR_ERR(dst2);
- if (err == -EPERM) {
- dst_release(dst);
- return dst2;
- } else
- goto relookup_failed;
+ return dst2;
+ }
+ err = PTR_ERR(dst2);
+ if (err == -EPERM) {
+ dst_release(dst);
+ return dst2;
}

relookup_failed: