mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Python: Avoid duplicate modules in points-to and resulting blow-up.
This commit is contained in:
@@ -195,6 +195,7 @@ class Module extends Module_, Scope, AstNode {
|
||||
|
||||
}
|
||||
|
||||
|
||||
bindingset[name]
|
||||
private predicate legalDottedName(string name) {
|
||||
name.regexpMatch("(\\p{L}|_)(\\p{L}|\\d|_)*(\\.(\\p{L}|_)(\\p{L}|\\d|_)*)*")
|
||||
@@ -244,3 +245,30 @@ private predicate isStubRoot(Folder f) {
|
||||
f.getAbsolutePath().matches("%/data/python/stubs")
|
||||
}
|
||||
|
||||
|
||||
/** Holds if the Container `c` should be the preferred file or folder for
|
||||
* the given name when performing imports.
|
||||
* Trivially true for any container if it is the only one with its name.
|
||||
* However, if there are several modules with the same name, then
|
||||
* this is the module most likely to be imported under that name.
|
||||
*/
|
||||
predicate isPreferredModuleForName(Container c, string name) {
|
||||
exists(int p |
|
||||
p = min(int x | x = priorityForName(_, name)) and
|
||||
p = priorityForName(c, name)
|
||||
)
|
||||
}
|
||||
|
||||
private int priorityForName(Container c, string name) {
|
||||
name = moduleNameFromFile(c) and
|
||||
(
|
||||
// In the source
|
||||
exists(c.getRelativePath()) and result = -1
|
||||
or
|
||||
// On an import path
|
||||
exists(c.getImportRoot(result))
|
||||
or
|
||||
// Otherwise
|
||||
result = 10000
|
||||
)
|
||||
}
|
||||
|
||||
@@ -47,12 +47,13 @@ cached newtype TObject =
|
||||
or
|
||||
/* Package objects */
|
||||
TPackageObject(Folder f) {
|
||||
exists(moduleNameFromFile(f))
|
||||
isPreferredModuleForName(f, _)
|
||||
}
|
||||
or
|
||||
/* Python module objects */
|
||||
TPythonModule(Module m) {
|
||||
not m.isPackage() and not exists(SyntaxError se | se.getFile() = m.getFile())
|
||||
not m.isPackage() and isPreferredModuleForName(m.getFile(), _) and
|
||||
not exists(SyntaxError se | se.getFile() = m.getFile())
|
||||
}
|
||||
or
|
||||
/* `True` */
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
| sqlite3 | 2 | 1 |
|
||||
| sqlite3.__init__ | 2 | 1 |
|
||||
| sqlite3.dump | 2 | 1 |
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
import python
|
||||
|
||||
from string name, int mcnt
|
||||
where mcnt = strictcount(Module m | m.getName() = name) and mcnt > 1
|
||||
select name, mcnt, strictcount(ModuleValue val | val.getName() = name)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
semmle-extractor-options: -R .
|
||||
optimize: true
|
||||
@@ -0,0 +1 @@
|
||||
import sqlite3.dump
|
||||
Reference in New Issue
Block a user