-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathHeapMemory.ql
More file actions
31 lines (27 loc) · 996 Bytes
/
HeapMemory.ql
File metadata and controls
31 lines (27 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @name Dynamic allocation after initialization
* @description Dynamic memory allocation (using malloc() or calloc()) should be confined to the initialization routines of a program.
* @kind problem
* @id cpp/jpl-c/heap-memory
* @problem.severity recommendation
* @tags resources
* external/jpl
*/
import cpp
class Initialization extends Function {
Initialization() {
// TODO: This could be refined to match precisely what functions count
// as "initialization", and are, hence, allowed to perform dynamic
// memory allocation.
this.getName().toLowerCase().matches("init%") or
this.getName().toLowerCase().matches("%\\_init")
}
}
class Allocation extends FunctionCall {
Allocation() { this.getTarget().getName() = ["malloc", "calloc", "alloca", "sbrk", "valloc"] }
}
from Function f, Allocation a
where
not f instanceof Initialization and
a.getEnclosingFunction() = f
select a, "Dynamic memory allocation is only allowed during initialization."