Files
codeql/python/ql/src/Variables/UninitializedLocal.ql
Taus Brock-Nannestad f07a7bf8cf Python: Autoformat everything using qlformat.
Will need subsequent PRs fixing up test failures (due to deprecated
methods moving around), but other than that everything should be
straight-forward.
2020-07-07 15:43:52 +02:00

37 lines
1.0 KiB
Plaintext

/**
* @name Potentially uninitialized local variable
* @description Using a local variable before it is initialized causes an UnboundLocalError.
* @kind problem
* @tags reliability
* correctness
* @problem.severity error
* @sub-severity low
* @precision medium
* @id py/uninitialized-local-variable
*/
import python
import Undefined
import semmle.python.pointsto.PointsTo
predicate uninitialized_local(NameNode use) {
exists(FastLocalVariable local | use.uses(local) or use.deletes(local) | not local.escapes()) and
(
any(Uninitialized uninit).taints(use) and
PointsToInternal::reachableBlock(use.getBasicBlock(), _)
or
not exists(EssaVariable var | var.getASourceUse() = use)
)
}
predicate explicitly_guarded(NameNode u) {
exists(Try t |
t.getBody().contains(u.getNode()) and
t.getAHandler().getType().pointsTo(ClassValue::nameError())
)
}
from NameNode u
where uninitialized_local(u) and not explicitly_guarded(u)
select u.getNode(), "Local variable '" + u.getId() + "' may be used before it is initialized."