Skip to content

Commit e757bce

Browse files
committed
Python: Fix up 'import failure' analysis query.
1 parent a6da499 commit e757bce

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

python/ql/src/analysis/ImportFailure.ql

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,37 @@ predicate ok_to_fail(ImportExpr ie) {
5757
os_specific_import(ie) != get_os()
5858
}
5959

60+
61+
class VersionTest extends @py_flow_node {
62+
63+
VersionTest() {
64+
exists(string name |
65+
name.matches("%version%") and
66+
this.(CompareNode).getAChild+().pointsTo(Module::named("sys").attr(name))
67+
)
68+
}
69+
70+
string toString() {
71+
result = "VersionTest"
72+
}
73+
74+
}
75+
76+
/** A guard on the version of the Python interpreter */
77+
class VersionGuard extends ConditionBlock {
78+
79+
VersionGuard() {
80+
this.getLastNode() instanceof VersionTest
81+
}
82+
83+
}
84+
6085
from ImportExpr ie
6186
where not ie.refersTo(_) and
6287
exists(Context c | c.appliesTo(ie.getAFlowNode())) and
6388
not ok_to_fail(ie) and
6489
not exists(VersionGuard guard |
65-
if guard.isTrue() then
66-
guard.controls(ie.getAFlowNode().getBasicBlock(), false)
67-
else
68-
guard.controls(ie.getAFlowNode().getBasicBlock(), true)
90+
guard.controls(ie.getAFlowNode().getBasicBlock(), _)
6991
)
7092

7193
select ie, "Unable to resolve import of '" + ie.getImportedModuleName() + "'."

0 commit comments

Comments
 (0)