mirror of
https://github.com/github/codeql.git
synced 2026-05-14 19:29:28 +02:00
Python: Port ModuleImportsItself.ql
Uses the existing machinery in ImportResolution.qll, after adding a few convenience predicates. The new modelling actually manages to find a result that the old points-to analysis did not. Apart from that there are no test changes.
This commit is contained in:
@@ -12,19 +12,19 @@
|
||||
*/
|
||||
|
||||
import python
|
||||
private import LegacyPointsTo
|
||||
import semmle.python.dataflow.new.DataFlow
|
||||
private import semmle.python.dataflow.new.internal.ImportResolution
|
||||
|
||||
predicate modules_imports_itself(ImportingStmt i, ModuleValue m) {
|
||||
i.getEnclosingModule() = m.getScope() and
|
||||
m =
|
||||
max(string s, ModuleValue m_ |
|
||||
s = i.getAnImportedModuleName() and
|
||||
m_.importedAs(s)
|
||||
|
|
||||
m_ order by s.length()
|
||||
)
|
||||
predicate modules_imports_itself(ImportingStmt i, Module m) {
|
||||
m = i.getEnclosingModule() and
|
||||
ImportResolution::importedBy(i, m) and
|
||||
// Exclude `from m import submodule` where the imported member is a submodule of m
|
||||
not exists(ImportMember im | im = i.(Import).getAName().getValue() |
|
||||
ImportResolution::getImmediateModuleReference(m).asExpr() = im.getModule() and
|
||||
ImportResolution::importedBy(i, any(Module sub | sub != m))
|
||||
)
|
||||
}
|
||||
|
||||
from ImportingStmt i, ModuleValue m
|
||||
from ImportingStmt i, Module m
|
||||
where modules_imports_itself(i, m)
|
||||
select i, "The module '" + m.getName() + "' imports itself."
|
||||
select i, "The module '" + ImportResolution::moduleName(m) + "' imports itself."
|
||||
|
||||
Reference in New Issue
Block a user