Skip to content

Commit f214f0e

Browse files
authored
Merge pull request #1589 from markshannon/python-fix-sanity
Python: Fix a couple of false positives in sanity query
2 parents 29e49ae + d750f38 commit f214f0e

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

python/ql/src/analysis/Sanity.ql

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ predicate ast_sanity(string clsname, string problem, string what) {
2525
clsname = a.getAQlClass() |
2626
uniqueness_error(count(a.toString()), "toString", problem) and what = "at " + a.getLocation().toString() or
2727
uniqueness_error(strictcount(a.getLocation()), "getLocation", problem) and what = a.getLocation().toString() or
28-
not exists(a.getLocation()) and problem = "no location" and what = a.toString()
28+
not exists(a.getLocation()) and not a.(Module).isPackage() and problem = "no location" and what = a.toString()
2929
)
3030
}
3131

@@ -54,8 +54,11 @@ predicate location_sanity(string clsname, string problem, string what) {
5454
predicate cfg_sanity(string clsname, string problem, string what) {
5555
exists(ControlFlowNode f |
5656
clsname = f.getAQlClass() |
57-
uniqueness_error(count(f.getNode()), "getNode", problem) and what = "at " + f.getLocation().toString() or
58-
not exists(f.getLocation()) and problem = "no location" and what = f.toString() or
57+
uniqueness_error(count(f.getNode()), "getNode", problem) and what = "at " + f.getLocation().toString()
58+
or
59+
not exists(f.getLocation()) and not exists(Module p | p.isPackage() | p.getEntryNode() = f or p.getAnExitNode() = f)
60+
and problem = "no location" and what = f.toString()
61+
or
5962
uniqueness_error(count(f.(AttrNode).getObject()), "getValue", problem) and what = "at " + f.getLocation().toString()
6063
)
6164
}
@@ -66,7 +69,7 @@ predicate scope_sanity(string clsname, string problem, string what) {
6669
uniqueness_error(count(s.getEntryNode()), "getEntryNode", problem) and what = "at " + s.getLocation().toString() or
6770
uniqueness_error(count(s.toString()), "toString", problem) and what = "at " + s.getLocation().toString() or
6871
uniqueness_error(strictcount(s.getLocation()), "getLocation", problem) and what = "at " + s.getLocation().toString() or
69-
not exists(s.getLocation()) and problem = "no location" and what = s.toString()
72+
not exists(s.getLocation()) and problem = "no location" and what = s.toString() and not s.(Module).isPackage()
7073
)
7174
}
7275

@@ -213,11 +216,14 @@ predicate file_sanity(string clsname, string problem, string what) {
213216
}
214217

215218
predicate class_value_sanity(string clsname, string problem, string what) {
216-
exists(ClassValue value |
217-
exists(value.getASuperType().lookup(what)) and
218-
not exists(value.lookup(what)) and
219+
exists(ClassValue value, ClassValue sup, string attr |
220+
what = value.getName() and
221+
sup = value.getASuperType() and
222+
exists(sup.lookup(attr)) and
223+
not value.failedInference(_) and
224+
not exists(value.lookup(attr)) and
219225
clsname = value.getAQlClass() and
220-
problem = "is missing attribute that superclass has"
226+
problem = "no attribute '" + attr + "', but super type '" + sup.getName() + "' does."
221227
)
222228
}
223229

python/ql/src/semmle/python/Scope.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Scope extends Scope_ {
7070

7171
/** Gets an exit from this Scope's control flow graph */
7272
ControlFlowNode getAnExitNode() {
73-
exists (int i | py_scope_flow(result, this, i) and i >= 0)
73+
exists (int i | py_scope_flow(result, this, i) and i >= 0)
7474
}
7575

7676
/** Gets an exit from this Scope's control flow graph,

0 commit comments

Comments
 (0)