-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUncheckedCastInEquals.ql
More file actions
32 lines (29 loc) · 1.1 KB
/
UncheckedCastInEquals.ql
File metadata and controls
32 lines (29 loc) · 1.1 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
/**
* @name Unchecked cast in Equals method
* @description The object passed as a parameter to 'Equals' is used in a cast without first checking its type, which can cause an unwanted 'InvalidCastException'.
* @kind problem
* @problem.severity warning
* @precision high
* @id cs/unchecked-cast-in-equals
* @tags reliability
* maintainability
*/
import csharp
import semmle.code.csharp.frameworks.System
predicate nodeBeforeParameterAccess(ControlFlow::Node node) {
exists(EqualsMethod equals | equals.getBody() = node.getElement())
or
exists(EqualsMethod equals, Parameter param, ControlFlow::Node mid |
equals.getParameter(0) = param and
equals.getAChild*() = mid.getElement() and
nodeBeforeParameterAccess(mid) and
not param.getAnAccess() = mid.getElement() and
mid.getASuccessor() = node
)
}
from ParameterAccess access, CastExpr cast
where
access = cast.getAChild() and
access.getTarget().getDeclaringElement() = access.getEnclosingCallable() and
nodeBeforeParameterAccess(access.getAControlFlowNode())
select cast, "Missing type-check before casting parameter to 'Equals'."