linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: Edward Cree <ecree@solarflare.com>
Cc: linux-sparse@vger.kernel.org
Subject: Re: Casting 0 to a __bitwise type
Date: Fri, 17 Feb 2017 13:37:46 +0100	[thread overview]
Message-ID: <20170217123744.jtog4rwpt6k6pyg3@macbook.local> (raw)
In-Reply-To: <5dcaea41-102c-ce1f-a4eb-6c1b917ba31c@solarflare.com>

On Fri, Feb 17, 2017 at 11:01:53AM +0000, Edward Cree wrote:
> (The actual code that is hitting this is drivers/net/ethernet/sfc/ethtool.c
>  in recent Linux trees.)
> From this I was able to create a new synthetic example, which *does*
>  reproduce the warning:
> 
>     typedef unsigned int __attribute__((bitwise)) __be32;
> 
>     static inline void ip6_fill_mask(__be32 *mask)
>     {
>         mask[0] = mask[1] = mask[2] = mask[3] = ~(__be32)0;
>     }
> 
>     static void f(void)
>     {
>         __be32 mask[4];
> 
>         ip6_fill_mask(mask);
>     }
> 
> Feeding the above program to sparse yields the output
> "test.c:5:51: warning: cast to restricted __be32"
> 
> In fact the example can be reduced even further:
> 
>     typedef unsigned int __attribute__((bitwise)) __be32;
> 
>     static __be32 foo = (__be32)0;
> 
> yields "test.c:3:22: warning: cast to restricted __be32"
> 
> My apologies for previously providing an incorrect example; I should have
>  tested it rather than assuming, as I did, that a static inline that was
>  never called would still be type-checked.

No worries.

In fact this warning is correct, by design.
Your not supposed to cast a bitwise type, even zero.
Here is a few examples that doesn't give a warning:
	mask[1] = 0;
	mask[2] = ~msk;
	mask[3] = ~(__force __be32) 0;
	mask[4] = ~({ __be32 msk = 0; msk; });

The canonical way to do what you need is via the (__force ...) cast.
In fact, in the driver you're talking about, just a few lines above
this fill_mask() function, there is a few masks already defined that
do exactly this, like IP4_ADDR_FULL_MASK.

Luc

> The information contained in this message is confidential and is intended for the addressee(s) only. If you have received this message in error, please notify the sender immediately and delete the message. Unless you are an addressee (or authorized to receive for an addressee), you may not use, copy or disclose to anyone this message or any information contained in this message. The unauthorized use, disclosure, copying or alteration of this message is strictly prohibited.
> --
> To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2017-02-17 12:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-14 21:36 Casting 0 to a __bitwise type Edward Cree
2017-02-14 22:51 ` Luc Van Oostenryck
2017-02-17 11:01   ` Edward Cree
2017-02-17 12:37     ` Luc Van Oostenryck [this message]
2017-02-17 13:13       ` Edward Cree
2017-02-17 14:16         ` Luc Van Oostenryck
2017-02-17 14:56           ` Edward Cree

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170217123744.jtog4rwpt6k6pyg3@macbook.local \
    --to=luc.vanoostenryck@gmail.com \
    --cc=ecree@solarflare.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).