Skip to content

Commit c767de0

Browse files
committed
Python: Refactor points-to origin code for better encapsulation.
1 parent 9caa9c1 commit c767de0

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

python/ql/src/semmle/python/pointsto/PointsTo.qll

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,27 @@ private import Filters as BaseFilters
3131
import semmle.dataflow.SSA
3232
private import MRO
3333

34-
/** Get a `ControlFlowNode` from an object or `here`.
35-
* If the object is a ControlFlowNode then use that, otherwise fall back on `here`
36-
*/
37-
pragma[inline]
38-
private ControlFlowNode origin_from_object_or_here(ObjectOrCfg object, ControlFlowNode here) {
39-
result = object
40-
or
41-
not object instanceof ControlFlowNode and result = here
34+
library class ObjectOrCfg extends @py_object {
35+
36+
string toString() {
37+
/* Not to be displayed */
38+
none()
39+
}
40+
41+
ControlFlowNode getOrigin() {
42+
result = this
43+
}
44+
45+
/** Get a `ControlFlowNode` from `this` or `here`.
46+
* If `this` is a ControlFlowNode then use that, otherwise fall back on `here`
47+
*/
48+
pragma[inline]
49+
ControlFlowNode fromObjectOrHere(ControlFlowNode here) {
50+
result = this
51+
or
52+
not this instanceof ControlFlowNode and result = here
53+
}
54+
4255
}
4356

4457
module PointsTo {
@@ -144,7 +157,7 @@ module PointsTo {
144157
ssa_variable_points_to(var, imp, obj, cls, orig) and
145158
imp.isImport() and
146159
obj != undefinedVariable() |
147-
origin = origin_from_object_or_here(orig, exit)
160+
origin = orig.fromObjectOrHere(exit)
148161
)
149162
or
150163
not exists(EssaVariable var | var.getAUse() = m.getANormalExit() and var.getSourceVariable().getName() = name) and
@@ -563,7 +576,7 @@ module PointsTo {
563576
exists(ObjectOrCfg origin_or_obj |
564577
value != undefinedVariable() and
565578
use_points_to_maybe_origin(f, context, value, cls, origin_or_obj) |
566-
origin = origin_from_object_or_here(origin_or_obj, f)
579+
origin = origin_or_obj.fromObjectOrHere(f)
567580
)
568581
}
569582

@@ -637,7 +650,7 @@ module PointsTo {
637650
exists(Object cls_or_mod, string name, ObjectOrCfg orig |
638651
receiver_object(f, context, cls_or_mod, name) and
639652
class_or_module_attribute(cls_or_mod, name, value, cls, orig) and
640-
origin = origin_from_object_or_here(orig, f)
653+
origin = orig.fromObjectOrHere(f)
641654
)
642655
or
643656
points_to(f.getObject(), context, unknownValue(), theUnknownType(), origin) and value = unknownValue() and cls = theUnknownType()
@@ -667,7 +680,7 @@ module PointsTo {
667680
private predicate from_import_points_to(ImportMemberNode f, PointsToContext context, Object value, ClassObject cls, ControlFlowNode origin) {
668681
exists(string name, ModuleObject mod, ObjectOrCfg orig |
669682
points_to(f.getModule(name), context, mod, _, _) and
670-
origin = origin_from_object_or_here(orig, f)
683+
origin = orig.fromObjectOrHere(f)
671684
|
672685
mod.getSourceModule() = f.getEnclosingModule() and
673686
exists(EssaVariable var |
@@ -2014,7 +2027,7 @@ module PointsTo {
20142027
exists(ObjectOrCfg obj |
20152028
Layer::module_attribute_points_to(mod, name, value, cls, obj) and
20162029
not exists(Variable v | v.getId() = name and v.getScope() = imp.getScope()) and
2017-
origin = origin_from_object_or_here(obj, imp)
2030+
origin = obj.fromObjectOrHere(imp)
20182031
)
20192032
or
20202033
/* Retain value held before import */

python/ql/src/semmle/python/types/ClassObject.qll

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ predicate is_c_metaclass(Object o) {
1010
}
1111

1212

13-
library class ObjectOrCfg extends @py_object {
14-
15-
string toString() {
16-
/* Not to be displayed */
17-
none()
18-
}
19-
20-
ControlFlowNode getOrigin() {
21-
result = this
22-
}
23-
24-
}
25-
2613
/** A class whose instances represents Python classes.
2714
* Instances of this class represent either builtin classes
2815
* such as `list` or `str`, or program-defined Python classes

0 commit comments

Comments
 (0)