0

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
1
  • 3 has type signed int. You should always be thinking about types when writing code containing shifts . Commented Jan 20, 2016 at 8:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, showed one more in my code, and one in XNU sources which would be harder to fix :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.