Skip to content

Conversation

jeongsoolee09
Copy link
Contributor

Description

please enter the description of your change here

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • rule number here
  • Queries have been modified for the following rules:
    • rule number here

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
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.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

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

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    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.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Copy link
Collaborator

@MichaelRFairhurst MichaelRFairhurst left a 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.
*/
/*
Copy link
Collaborator

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 |
Copy link
Collaborator

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 */
Copy link
Collaborator

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.
*/

/**
Copy link
Collaborator

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()
Copy link
Collaborator

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 {
Copy link
Collaborator

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 {
Copy link
Collaborator

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
Copy link
Collaborator

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()
Copy link
Collaborator

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(
Copy link
Collaborator

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()
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants