lundman
lundman

Reputation: 1726

Warning for clang's shifting bare integer values default to 32 bit

So, in the upstream sources, we have this line

rc_c_max = allmem - (3 << 30);

Upstream which typically uses gcc, does things "as expected". allmem is uint64_t. But with clang, it appears to assume "3" is 32 bit (or maybe signed) and the results are not "as expected". An easy fix for it is to change it to

rc_c_max = allmem - (3ULL << 30);

and everything works. Changing all the bare-interger shifts, by working out/guessing which needs to be corrected is tedious, is there a warning I can enable for clang to point this out? -Wall -Wextra does not complain about this line.

Output:

1/19/16 9:25:30.000 AM kernel[0]: allmem - (3 << 30) : 0x373333000 : 14817636352
1/19/16 9:25:30.000 AM kernel[0]: allmem - (3ULL << 30) : 0x273333000 : 10522669056

Upvotes: 0

Views: 214

Answers (1)

Trevor Hickey
Trevor Hickey

Reputation: 37914

The flag you are looking for is:

-Wshift-sign-overflow  

Although, unlike gcc, I recommend using clang's compiler flag:

-Weverything  

That would enable -Wshift-sign-overflow as well.

Upvotes: 2

Related Questions