Update init calls subclass to not use pointto

This commit is contained in:
Joe Farebrother
2025-06-09 14:03:22 +01:00
parent 781f78813f
commit ed3cf84efd
4 changed files with 42 additions and 30 deletions

View File

@@ -0,0 +1,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()

View File

@@ -1,30 +0,0 @@
/**
* @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
from
ClassObject supercls, string method, Call call, FunctionObject overriding,
FunctionObject overridden
where
exists(FunctionObject init, SelfAttribute sa |
supercls.declaredAttribute("__init__") = init and
call.getScope() = init.getFunction() and
call.getFunc() = sa
|
sa.getName() = method and
overridden = supercls.declaredAttribute(method) and
overriding.overrides(overridden)
)
select call, "Call to self.$@ in __init__ method, which is overridden by $@.", overridden, method,
overriding, overriding.descriptiveString()