Skip to content
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
28 changes: 28 additions & 0 deletions cpp/ql/src/Likely Bugs/Memory Management/AllocaInLoop.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @name alloca in a loop
* @description Using alloca in a loop can lead to a stack overflow
* @kind problem
* @problem.severity warning
* @precision medium
* @id cpp/alloca-in-loop
* @tags reliability
* correctness
* external/cwe/cwe-770
*/
import cpp

Loop getAnEnclosingLoopOfExpr(Expr e) {
result = e.getEnclosingStmt().getParent*() or
result = getAnEnclosingLoopOfStmt(e.getEnclosingStmt())
}

Loop getAnEnclosingLoopOfStmt(Stmt s) {
result = s.getParent*() or
result = getAnEnclosingLoopOfExpr(s.getParent*())
}

from Loop l, FunctionCall fc
where getAnEnclosingLoopOfExpr(fc) = l
and fc.getTarget().getName() = "__builtin_alloca"
and not l.(DoStmt).getCondition().getValue() = "0"
select fc, "Stack allocation is inside a $@ and could lead to overflow.", l, l.toString()