-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSpinOnField.ql
More file actions
66 lines (60 loc) · 1.67 KB
/
SpinOnField.ql
File metadata and controls
66 lines (60 loc) · 1.67 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @name Spin on field
* @description Repeatedly reading a non-volatile field within the condition of an empty loop may
* result in an infinite loop.
* @kind problem
* @problem.severity warning
* @precision medium
* @id java/spin-on-field
* @tags quality
* reliability
* correctness
* concurrency
* performance
*/
import java
/** A numerical comparison or an equality test. */
class ComparisonOrEqTestExpr extends Expr {
ComparisonOrEqTestExpr() {
this instanceof ComparisonExpr or
this instanceof ReferenceEqualityTest
}
}
/** An empty statement or block. */
class Empty extends Stmt {
Empty() {
this instanceof EmptyStmt or
this.(BlockStmt).getNumStmt() = 0
}
}
/** An empty loop statement. */
class EmptyLoop extends Stmt {
EmptyLoop() {
exists(ForStmt stmt | stmt = this |
not exists(stmt.getAnInit()) and
not exists(stmt.getAnUpdate()) and
stmt.getBody() instanceof Empty
)
or
this.(WhileStmt).getBody() instanceof Empty
or
this.(DoStmt).getBody() instanceof Empty
}
Expr getCondition() {
result = this.(ForStmt).getCondition() or
result = this.(WhileStmt).getCondition() or
result = this.(DoStmt).getCondition()
}
}
from EmptyLoop loop, FieldAccess access, Field field, ComparisonOrEqTestExpr expr
where
loop.getCondition() = expr and
access.isOwnFieldAccess() and
access.getParent() = expr and
field = access.getVariable() and
field.isStatic() and
not field.isFinal() and
not field.isVolatile() and
field.getType() instanceof RefType
select access,
"Spinning on " + field.getName() + " in " + loop.getEnclosingCallable().getName() + "."