-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathInitCallsSubclassMethod.ql
More file actions
42 lines (38 loc) · 1.42 KB
/
InitCallsSubclassMethod.ql
File metadata and controls
42 lines (38 loc) · 1.42 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
/**
* @name `__init__` method calls overridden method
* @description Calling a method from `__init__` that is overridden by a subclass may result in a partially
* initialized instance being observed.
* @kind problem
* @tags reliability
* correctness
* @problem.severity warning
* @sub-severity low
* @precision high
* @id py/init-calls-subclass
*/
import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.internal.DataFlowDispatch
predicate initSelfCall(Function init, DataFlow::MethodCallNode call) {
init.isInitMethod() and
call.getScope() = init and
exists(DataFlow::Node self, DataFlow::ParameterNode selfArg |
call.calls(self, _) and
selfArg.getParameter() = init.getArg(0) and
DataFlow::localFlow(selfArg, self)
)
}
predicate initSelfCallOverridden(Function init, DataFlow::MethodCallNode call, Function override) {
initSelfCall(init, call) and
exists(Class superclass, Class subclass |
superclass = init.getScope() and
subclass = override.getScope() and
subclass = getADirectSubclass+(superclass) and
call.calls(_, override.getName())
)
}
from Function init, DataFlow::MethodCallNode call, Function override
where initSelfCallOverridden(init, call, override)
select call,
"This call to " + override.getName() + " in initialization method is overridden by " +
override.getScope().getName() + ".$@.", override, override.getName()