-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCoderResult-throwException-on-wrong-variable.ql
More file actions
93 lines (80 loc) · 2.98 KB
/
Copy pathCoderResult-throwException-on-wrong-variable.ql
File metadata and controls
93 lines (80 loc) · 2.98 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Finds calls to `CoderResult.throwException()` preceded by a `CoderResult`
* error check call (e.g. `CoderResult.isError()`) performed on a different
* variable; this might indicate that variables were interchanged.
* E.g.:
* ```
* CoderResult result = ...;
* ...
* CoderResult result2 = ...;
* if (result2.isError()) {
* // Called on the wrong variable; `result2` was checked
* result.throwException();
* }
* ```
*/
import java
class TypeCoderResult extends Class {
TypeCoderResult() {
hasQualifiedName("java.nio.charset", "CoderResult")
}
}
class CoderResultMethod extends Method {
CoderResultMethod() {
getDeclaringType() instanceof TypeCoderResult
}
}
abstract class CoderResultErrorCheckingMethod extends CoderResultMethod {
}
class IsErrorMethod extends CoderResultErrorCheckingMethod {
IsErrorMethod() {
hasStringSignature("isError()")
}
}
class IsMalformedMethod extends CoderResultErrorCheckingMethod {
IsMalformedMethod() {
hasStringSignature("isMalformed()")
}
}
class IsUnmappableMethod extends CoderResultErrorCheckingMethod {
IsUnmappableMethod() {
hasStringSignature("isUnmappable()")
}
}
class ThrowExceptionMethod extends CoderResultMethod {
ThrowExceptionMethod() {
hasStringSignature("throwException()")
}
}
abstract class CoderResultNoErrorCheckingMethod extends CoderResultMethod {
}
class IsOverflowMethod extends CoderResultNoErrorCheckingMethod {
IsOverflowMethod() {
hasStringSignature("isOverflow()")
}
}
class IsUnderflowMethod extends CoderResultNoErrorCheckingMethod {
IsUnderflowMethod() {
hasStringSignature("isUnderflow()")
}
}
private predicate areCloseTogether(Expr first, Expr second) {
second.getLocation().getStartLine() - first.getLocation().getEndLine() < 5
}
from MethodAccess coderResultCheck, VarAccess resultCheckVarRead, ConditionNode conditionNode, boolean branch,
MethodAccess throwExceptionCall, VarAccess throwVarRead
where
coderResultCheck.getQualifier() = resultCheckVarRead
and conditionNode.getCondition() = coderResultCheck
and throwExceptionCall.getQualifier() = throwVarRead
and exists(CoderResultMethod m | m = coderResultCheck.getMethod() |
m instanceof CoderResultNoErrorCheckingMethod and branch = false
or m instanceof CoderResultErrorCheckingMethod and branch = true
)
and throwExceptionCall.getMethod() instanceof ThrowExceptionMethod
and conditionNode.getABranchSuccessor(branch).getASuccessor*() = throwExceptionCall.getControlFlowNode()
// Make sure check and throw call are close together, otherwise they might be unrelated
and areCloseTogether(coderResultCheck, throwExceptionCall)
and resultCheckVarRead.getVariable() != throwVarRead.getVariable()
select throwVarRead, "Throws exception for $@, but $@ check is performed for $@",
throwVarRead, throwVarRead.toString(), coderResultCheck, "this", resultCheckVarRead, resultCheckVarRead.toString()