-
Notifications
You must be signed in to change notification settings - Fork 68
Implement "Statements" package #938
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
base: main
Are you sure you want to change the base?
Conversation
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.
This is really coming along and looking really good!!
* to a non-const reference variable (thus constituting a `T` -> `&T` conversion.), i.e. | ||
* initialization and assignment. | ||
*/ | ||
/* |
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.
Simple comment formatting, unnecessary split
predicate loopVariableAssignedToNonConstPointerOrReferenceType( | ||
ForStmt forLoop, VariableAccess loopVariableAccessInCondition | ||
) { | ||
exists(Expr assignmentRhs, DerivedType targetType | |
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.
Likely want to test that this works for a int * const x
:
void f(int * const x) {
(*x)++;
}
int main() {
for (int i = 0; i < 10; ++i) {
f(&i);
std::cout << i << std::endl;
}
}
I believe what will happen is that int * const x
will be a DerivedType
of type SpecifiedType
with a const specifier. A SpecifiedType
is not instanceof PointerType
or instanceof ReferenceType
and so this predicate will not hold, even though the value of i
is modifiable within f
.
You may also have problems with typedefs, such as typedef int *int_ptr_t
for the same reason.
The solution here I believe will be to call .getUnderlyingType()
. Another option frequently used for this is .stripSpecifiers()
. Each of these will remove the const and resolve the typedef. I think .stripSpecifiers()
may remove the const in const int*
, though, which would make it unsuitable here.
assignmentRhs.(AddressOfExpr).getOperand() = | ||
loopVariableAccessInCondition.getTarget().getAnAccess() | ||
or | ||
/* 2. The address is taken: A loop variable access */ |
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.
Should this say a reference is taken?
* Also, this predicate requires that the call is the body of the given for-loop. | ||
*/ | ||
|
||
/** |
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.
I would attach these two doc block comments together so that we don't lose the connection if adding code or reordering predicates etc.
*/ | ||
|
||
/* 1. Get the expression `E` when the update expression is `i += E` or `i -= E`. */ | ||
result = forLoop.getUpdate().getAChild*().(AssignAddExpr).getRValue() |
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.
You can use AssignAddOrSubExpr
and .getLoopStep()
!
* condition of a legacy for-loop. It is characterized by a value read from a variable being | ||
* compared to a value, which is supposed to be the loop bound. | ||
*/ | ||
class LegacyForLoopCondition extends RelationalOperation { |
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.
Consider moving this into codingstandards.cpp.Loops
abstract Expr getLoopStep(); | ||
} | ||
|
||
class CrementLegacyForLoopUpdateExpression extends LegacyForLoopUpdateExpression { |
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.
Consider taking this class, and AssignAddOrSubExpr
and AddOrSubThenAssignExpr
and moving them to a file such as cpp/common/src/codingstandards/cpp/ast/Increment.qll
.
I'd decouple it from loops, for instance, rename getLoopStep
to getIncremented
or something like that.
targetType instanceof ReferenceType | ||
) | ||
| | ||
assignmentRhs.getEnclosingStmt().getParent*() = forLoop.getStmt() and |
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.
You may want to add not assignmentRhs.isInUnevaluatedContext()
for safety.
That would prevent reporting cases like sizeof(g(&i))
or decltype(g(&i))
.
loopCounterType = forLoopCondition.getLoopCounter().getType() and | ||
loopBoundType = forLoopCondition.getLoopBound().getType() | ||
| | ||
loopCounterType.getSize() < loopBoundType.getSize() |
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.
Two missed cases here:
- Mixing signed/unsigned types, they may have the same size but they'll hold different ranges.
- The type and runtime value may lead to different conclusions.
I think you may be able to get away with upperBound(loopCounter) < upperBound(loopBound)
. That would handle signedness, constants (like x < 10ull
), and dynamic ranges (like unsigned long long bound = 10; ... x < bound
).
* variable that is passed as reference to a non-const reference parameter of a function, | ||
* constituting a `T` -> `&T` conversion. | ||
*/ | ||
predicate loopVariablePassedAsArgumentToNonConstReferenceParameter( |
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 thought on simplifying these names.
Maybe instead of naming them loopVariablePassedAs...
you can rename them to passedAsNonConstReference
/passedAsNonConstPointer
and remove the ForStmt
argument.
Then at the call sites you can change
loopVariablePassedAsArgumentToNonConstReferenceParameter(loop, va)
// becomes
exists(VariableAccess other |
passedAsNonConstReference(other) and
other.getVariable() = loop.getBound().(VariableAccess).getVariable() and
other.getEnclosingStmt().getParent*() = loop.getStmt()
)
Description
please enter the description of your change here
Change request type
.ql
,.qll
,.qls
or unit tests)Rules with added or modified queries
Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
Reviewer
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.