Skip to content
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++: Avoid a CP in cpp/alloca-in-loop #16563

Merged
merged 1 commit into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions cpp/ql/src/Likely Bugs/Memory Management/AllocaInLoop.ql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ class AllocaCall extends FunctionCall {
}
}

/**
* Gets an expression associated with a dataflow node.
*/
private Expr getExpr(DataFlow::Node node) {
result = node.asInstruction().getAst()
or
result = node.asOperand().getUse().getAst()
or
result = node.(DataFlow::RawIndirectInstruction).getInstruction().getAst()
or
result = node.(DataFlow::RawIndirectOperand).getOperand().getUse().getAst()
}

/**
* A loop that contains an `alloca` call.
*/
Expand Down Expand Up @@ -185,19 +198,6 @@ class LoopWithAlloca extends Stmt {
not this.conditionReachesWithoutUpdate(var, this.(Loop).getCondition())
}

/**
* Gets an expression associated with a dataflow node.
*/
private Expr getExpr(DataFlow::Node node) {
result = node.asInstruction().getAst()
or
result = node.asOperand().getUse().getAst()
or
result = node.(DataFlow::RawIndirectInstruction).getInstruction().getAst()
or
result = node.(DataFlow::RawIndirectOperand).getOperand().getUse().getAst()
}

/**
* Gets a definition that may be the most recent definition of the
* controlling variable `var` before this loop.
Expand All @@ -210,7 +210,7 @@ class LoopWithAlloca extends Stmt {
// Phi nodes will be preceded by nodes that represent actual definitions
not result instanceof DataFlow::SsaPhiNode and
// A source is outside the loop if it's not inside the loop
not exists(Expr e | e = this.getExpr(result) | this = getAnEnclosingLoopOfExpr(e))
not exists(Expr e | e = getExpr(result) | this = getAnEnclosingLoopOfExpr(e))
)
}

Expand All @@ -221,9 +221,9 @@ class LoopWithAlloca extends Stmt {
private int getAControllingVarInitialValue(Variable var, DataFlow::Node source) {
source = this.getAPrecedingDef(var) and
(
result = this.getExpr(source).getValue().toInt()
result = getExpr(source).getValue().toInt()
or
result = this.getExpr(source).(Assignment).getRValue().getValue().toInt()
result = getExpr(source).(Assignment).getRValue().getValue().toInt()
)
}

Expand Down
Loading