-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathInfiniteLoop.ql
More file actions
23 lines (21 loc) · 840 Bytes
/
InfiniteLoop.ql
File metadata and controls
23 lines (21 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @name Infinite loop
* @description Updating a loop index with a compound assignment
* could cause non-termination.
* @kind problem
* @problem.severity warning
* @id apple-xnu/cpp/infinite-loop
*/
import cpp
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
// Find loops like this:
// while (x) { ...; x -= n; }
from Loop loop, Variable v, AssignArithmeticOperation assign
where (loop.getCondition() = v.getAnAccess() or
loop.getCondition().(ComparisonOperation).getAnOperand() = v.getAnAccess())
and assign.getLValue() = v.getAnAccess()
// Compound assignment is in the body of the loop:
and assign = loop.getStmt().getAChild*()
and lowerBound(assign.getRValue()) <= 0
and upperBound(assign.getRValue()) >= 0
select loop, "Loop might not terminate due to this $@.", assign, "assignment"