Left shifting a signed positive value is undefined if the result is not representable in the unsigned version of the return type.
The analyzer now returns an UndefVal in this case and UndefResultChecker is updated to warn about it.
Differential D41816
[analyzer] Model and check unrepresentable left shifts rnkovacs on Jan 8 2018, 5:24 AM. Authored by
Details Left shifting a signed positive value is undefined if the result is not representable in the unsigned version of the return type. The analyzer now returns an UndefVal in this case and UndefResultChecker is updated to warn about it.
Diff Detail Event TimelineComment Actions Overall looks good to me, one comment inline. I think it is good to have these checks to prevent the analyzer executing undefined behavior. Maybe this would make it more feasible to run the analyzer with ubsan :)
Comment Actions Looks great. @dcoughlin: would you approve the warning message text? Maybe actually we could print out the exact numbers that cause the bit shift to overflow, since we do have them when we check. Comment Actions The diagnostic text looks to me, but I do have a comment about the nested 'if' inline.
|
This inner 'if' looks fishy to me because if the 'else' branch is ever taken then OS will be empty.
If the else branch can't be taken then you should turn it into an assert(). If it can be taken, then you should make sure that the fall through goes through the "default" else case at the bottom. One way to do this is to pull out the "is representable logic" into a helper function and call that in the containing 'else if'.
Something like: