-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: Add barriers to cpp/uncontrolled-allocation-size
#5903
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++: Add barriers to cpp/uncontrolled-allocation-size
#5903
Conversation
@@ -139,7 +139,7 @@ void more_bounded_tests() { | |||
|
|||
if (size > 0) | |||
{ | |||
malloc(size * sizeof(int)); // BAD | |||
malloc(size * sizeof(int)); // GOOD (overflow not possible) |
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.
Could we add a test with a long
to keep testing size > 0
with an actual overflow?
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.
Good point. I've done this in 31091c6. Sorry about the horrible diff from that commit. All it's doing is move a few lines and add the correct BAD
result to the .expected file.
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.
LGTM.
// Subtracting two pointers is either well-defined (and the result will likely be small), or | ||
// terribly undefined and dangerous. Here, we assume that the programmer has ensured that the | ||
// result is well-defined (i.e., the two pointers point to the same object), and thus the result | ||
// will likely be small. |
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.
Seems reasonable. If the subtraction result is not well-defined, that should probably be handled by another query (which may or may-not already exist) rather than blaming a later malloc
.
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. 👍 I've opened up https://github.com/github/codeql-c-team/issues/525 for this exact query idea.
(Part of https://github.com/github/codeql-c-team/issues/272.)
This PR adds two barriers to
cpp/uncontrolled-allocation-size
:PointerDiffExpr
. This is quite a common cause of false positives for this query. When I compare 5031b73 to 2d0a561) we get:This change doesn't seem to affect SAMATE.