|
| 1 | +/** |
| 2 | + * @name `__init__` method calls overridden method |
| 3 | + * @description Calling a method from `__init__` that is overridden by a subclass may result in a partially |
| 4 | + * initialized instance being observed. |
| 5 | + * @kind problem |
| 6 | + * @tags reliability |
| 7 | + * correctness |
| 8 | + * @problem.severity warning |
| 9 | + * @sub-severity low |
| 10 | + * @precision high |
| 11 | + * @id py/init-calls-subclass |
| 12 | + */ |
| 13 | + |
| 14 | +import python |
| 15 | +import semmle.python.dataflow.new.DataFlow |
| 16 | +import semmle.python.dataflow.new.internal.DataFlowDispatch |
| 17 | + |
| 18 | +predicate initSelfCall(Function init, DataFlow::MethodCallNode call) { |
| 19 | + init.isInitMethod() and |
| 20 | + call.getScope() = init and |
| 21 | + exists(DataFlow::Node self, DataFlow::ParameterNode selfArg | |
| 22 | + call.calls(self, _) and |
| 23 | + selfArg.getParameter() = init.getArg(0) and |
| 24 | + DataFlow::localFlow(selfArg, self) |
| 25 | + ) |
| 26 | +} |
| 27 | + |
| 28 | +predicate initSelfCallOverridden(Function init, DataFlow::MethodCallNode call, Function override) { |
| 29 | + initSelfCall(init, call) and |
| 30 | + exists(Class superclass, Class subclass | |
| 31 | + superclass = init.getScope() and |
| 32 | + subclass = override.getScope() and |
| 33 | + subclass = getADirectSubclass+(superclass) and |
| 34 | + call.calls(_, override.getName()) |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +from Function init, DataFlow::MethodCallNode call, Function override |
| 39 | +where initSelfCallOverridden(init, call, override) |
| 40 | +select call, |
| 41 | + "This call to " + override.getName() + " in initialization method is overridden by " + |
| 42 | + override.getScope().getName() + ".$@.", override, override.getName() |
0 commit comments