Python: Port UninitializedLocal.ql

Uses the new ExceptionTypes machinery. No test changes.
This commit is contained in:
Taus
2026-02-26 15:23:12 +00:00
parent 28eec77cd8
commit 5ba6211ec2
2 changed files with 9 additions and 4 deletions

View File

@@ -2322,6 +2322,11 @@ module ExceptionTypes {
BaseException() { name = "BaseException" }
}
/** The builtin `NameError` exception type. */
class NameError extends BuiltinExceptType {
NameError() { name = "NameError" }
}
/**
* Holds if the exception edge from `r` to `handler` is unlikely because
* none of the exception types that `r` may raise are handled by `handler`.

View File

@@ -12,7 +12,7 @@
*/
import python
private import LegacyPointsTo
private import semmle.python.dataflow.new.internal.DataFlowDispatch
import Undefined
predicate uninitialized_local(NameNode use) {
@@ -21,16 +21,16 @@ predicate uninitialized_local(NameNode use) {
) and
(
any(Uninitialized uninit).taints(use) and
PointsToInternal::reachableBlock(use.getBasicBlock(), _)
Reachability::likelyReachable(use.getBasicBlock())
or
not exists(EssaVariable var | var.getASourceUse() = use)
)
}
predicate explicitly_guarded(NameNode u) {
exists(Try t |
exists(Try t, ExceptionTypes::NameError nameError |
t.getBody().contains(u.getNode()) and
t.getAHandler().getType().(ExprWithPointsTo).pointsTo(ClassValue::nameError())
nameError.getAUse().asExpr() = t.getAHandler().getType()
)
}