Skip to content

Commit f09f1c4

Browse files
committed
Python: Minor refactor in PoorMansFunctionResolution
1 parent 9e097f5 commit f09f1c4

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

python/ql/lib/semmle/python/frameworks/internal/PoorMansFunctionResolution.qll

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ private DataFlow::TypeTrackingNode poorMansFunctionTracker(DataFlow::TypeTracker
6262
exists(DataFlow::TypeTracker t2 | result = poorMansFunctionTracker(t2, func).track(t2, t))
6363
}
6464

65+
/**
66+
* Gets a reference to `func`. `func` must be defined inside a class, and the reference
67+
* will be inside a different method of the same class.
68+
*/
69+
private DataFlow::Node getSimpleMethodReferenceWithinClass(Function func) {
70+
// TODO: Should take MRO into account
71+
exists(Class cls, Function otherFunc, DataFlow::Node selfRefOtherFunc |
72+
cls.getAMethod() = func and
73+
cls.getAMethod() = otherFunc
74+
|
75+
selfRefOtherFunc.getALocalSource().(DataFlow::ParameterNode).getParameter() =
76+
otherFunc.getArg(0) and
77+
result.(DataFlow::AttrRead).accesses(selfRefOtherFunc, func.getName())
78+
)
79+
}
80+
6581
/**
6682
* INTERNAL: Do not use.
6783
*
@@ -95,13 +111,5 @@ private DataFlow::TypeTrackingNode poorMansFunctionTracker(DataFlow::TypeTracker
95111
DataFlow::Node poorMansFunctionTracker(Function func) {
96112
poorMansFunctionTracker(DataFlow::TypeTracker::end(), func).flowsTo(result)
97113
or
98-
// simple method calls within a class
99-
// TODO: Should take MRO into account
100-
exists(Class cls, Function otherFunc, DataFlow::Node selfRef |
101-
cls.getAMethod() = func and
102-
cls.getAMethod() = otherFunc
103-
|
104-
selfRef.getALocalSource().(DataFlow::ParameterNode).getParameter() = otherFunc.getArg(0) and
105-
result.(DataFlow::AttrRead).accesses(selfRef, func.getName())
106-
)
114+
result = getSimpleMethodReferenceWithinClass(func)
107115
}

0 commit comments

Comments
 (0)