Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9401fc

Browse files
committed
CPP: add a query for catching alloca in a loop
Thanks to Sam Lanning (@samlanning) and Robert Marsh for taking the time to help to make it possible. In fact, it was Robert Marsh who effectively wrote the query and figured out that __builtin_alloca should be used to also take functions like strdupa into account. I just filled out the metadata :-)
1 parent f5e419e commit e9401fc

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name alloca in a loop
3+
* @description Using alloca in a loop can lead to a stack overflow
4+
* @kind problem
5+
* @problem.severity warning
6+
* @precision medium
7+
* @id cpp/alloca-in-loop
8+
* @tags reliability
9+
* correctness
10+
* external/cwe/cwe-770
11+
*/
12+
import cpp
13+
14+
Loop getAnEnclosingLoopOfExpr(Expr e) {
15+
result = e.getEnclosingStmt().getParent*() or
16+
result = getAnEnclosingLoopOfStmt(e.getEnclosingStmt())
17+
}
18+
19+
Loop getAnEnclosingLoopOfStmt(Stmt s) {
20+
result = s.getParent*() or
21+
result = getAnEnclosingLoopOfExpr(s.getParent*())
22+
}
23+
24+
from Loop l, FunctionCall fc
25+
where getAnEnclosingLoopOfExpr(fc) = l
26+
and fc.getTarget().getName() = "__builtin_alloca"
27+
and not l.(DoStmt).getCondition().getValue() = "0"
28+
select fc, "Stack allocation is inside a $@ and could lead to overflow.", l, l.toString()

0 commit comments

Comments
 (0)