Re: question regarding sparse warning about cast truncate

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Mar 03, 2019 at 10:46:35AM +0200, Oded Gabbay wrote:
> Hello,
> I'm cleaning up my driver using sparse and I wanted to ask you about a
> warning I get regarding cast truncate:
> 
> drivers/misc/habanalabs/goya/goya.c:606:44: warning: cast truncates
> bits from constant value (7ff0000000 becomes f0000000)
> 
> I get this warning (and a lot more like it), whenever I use the
> generic kernel macro of "lower_32_bits". This macro effectively puts
> (u32) before the variable/define. The variable or define are 64-bits
> (u64 or ull).
> Now, that is the purpose of this macro - to truncate the 64-bit to
> 32-bit. We use it all the time to write registers. Why do I get a
> sparse warning about this ? This is the expected behavior.
> 
> Could you please tell me what am I missing?

[CC to Sparse's mailing list]

Hi,


Well, in most (non-generic) code it's quite unusual to cast
a *constant* to a smaller type. When it happens it may be
a sign that a typing error is done or some other problem.

But for sure when you have code where you need to write
a constant 64-bit value as 2 32-bit ones or similar,
for example in some very generic code/macro then this
warning is just noise and annoying.

This warning also happens in code like:
	switch(size) {
	case 1: ... (u8) x; break
	case 2: ... (u16) x; break
	case 4: ... (u32) x; break
	}
with x, for example, being 0x100 and size 2. Then no cast
is effectively done but the warning is nevertheless given
for the (u8).

It should be noted that the problem with lower_32_bits()
could be avoided by first masking it with 0xffffffff
(to me the masking correspond better to 'taking the lower
bits' than the cast which also/mainly change the type).
Same for the example just above with the appropriate
mask. The generated code should be the same but from
what I've understood from previous discussions on the
subject some are strongly opposed to that.

I have no idea how useful this warning really is, how often
it allows to catch a bug or some suspiscious code.
I would not be opposed at all o conditionalize it with some
warning flag like '-Wcast-truncates-constant'. The real
question is if it should default to 'on' or to 'off'.


-- Luc



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux