-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: Recognize any non-overflowing arithmetic expression as a barrier for cpp/uncontrolled-arithmetic
#6120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++: Recognize any non-overflowing arithmetic expression as a barrier for cpp/uncontrolled-arithmetic
#6120
Conversation
This is similar to something we did on another query recently, right? |
Yes. We did this in #5903. Once this PR is merged I'll pull the |
Sounds good. Can you point me to some LGTM projects for which this query has results? |
Our list of usual suspect has some results: https://lgtm.com/query/6937375284332555889/. In terms of where this PR makes a difference, you can see an instance of this on the The other projects in that run are similar cases which sadly need a more syntactic barrier which I haven't added yet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments.
Can we get rid of any of the boundedDiv
cases in bounded
now?
e instanceof UnaryArithmeticOperation or | ||
e instanceof BinaryArithmeticOperation | ||
) and | ||
not convertedExprMightOverflow(e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm understanding correctly, this can in certain cases fall short of the specification "greatly reduces the range of possible values" - to give an extreme example the following is now a barrier:
x = x + 0; // this can't overflow
However this is pretty silly, in practice we expect the overwhelming majority of arithmetic either can cause an overflow (x = x + 1
), or greatly narrows the range of values (x = x / RAND_MAX
) ... or is working on an already constrained value (hopefully safely).
If my above understanding is accurate, this is a somewhat heuristic approach, but I think I'm happy with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. This whole predicate uses a bunch of heuristics.
Unfortunately not. |
bb6a2d3
to
a611e76
Compare
@geoffw0 I believe all your concerns have been addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, thanks for the fixes, merging...
This should be one of the final low-hanging fruits for this query that's motivated by real-world FPs.
(Part of https://github.com/github/codeql-c-team/issues/272.)