mirror of
https://github.com/github/codeql.git
synced 2026-04-30 11:15:13 +02:00
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 :-)
This commit is contained in:
28
cpp/ql/src/Likely Bugs/Memory Management/AllocaInLoop.ql
Normal file
28
cpp/ql/src/Likely Bugs/Memory Management/AllocaInLoop.ql
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user