[PATCH] sparse: fix erroneous 'cast truncates bits' warning

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

 



With the following code:

enum foo {
	BAR = 1ULL << 63,
};

I was seeing the following warning:

enum.c:2:20: warning: cast truncates bits from constant value (8000000000000000 becomes 0)

parse_enum_declaration() keeps track of the minimum and maximum values of
the enum's values and tries to find a data type that contain it. In this
case it ought to match 'unsigned long long', but it doesn't. The reason is
that ullong_ctype->bit_size == 64 and type_is_ok() will do a left shift of
64 on the maximum/minimum values, which is undefined in C.

My compiler (gcc 5.5.0) gives a non-zero value for (1ULL << 63) >> 64, and
the enum's underlying type becomes bad_ctype which has ->bit_size == -1,
thus failing the cast truncation test in cast_value().
---
 parse.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git parse.c parse.c
index 02a55a7..4193a0f 100644
--- parse.c
+++ parse.c
@@ -786,6 +786,8 @@ static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
 	int shift = type->bit_size;
 	int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
 
+	if (shift == 64)
+		return 1;
 	if (!is_unsigned)
 		shift--;
 	if (upper->x == 0 && upper->y >> shift)
-- 
2.16.1.72.g5be1f00a9.dirty




[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