Skip to content

Commit 1251bc5

Browse files
committed
Python: Fix bad join in TObject::literal_instantiation
Here, `context.appliesTo(n)` was being distributed across all of the disjuncts, which caused poor performance. The new helper predicate, `literal_node_class` should be fairly small, since it only applies to a subset of `ControlFlowNode`s, and only assigns a limited set of `ClassObjectInternal`s to these nodes.
1 parent 35a63e2 commit 1251bc5

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

python/ql/src/semmle/python/objects/TObject.qll

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,32 @@ predicate class_method(
229229
PointsToInternal::pointsTo(instantiation.getArg(0), context, function, _)
230230
}
231231

232+
/**
233+
* Holds if the literal corresponding to the control flow node `n` has class `cls`.
234+
*
235+
* Helper predicate for `literal_instantiation`. Prevents a bad join with
236+
* `PointsToContext::appliesTo` from occuring.
237+
*/
238+
pragma[nomagic]
239+
private predicate literal_node_class(ControlFlowNode n, ClassObjectInternal cls) {
240+
n instanceof ListNode and cls = ObjectInternal::builtin("list")
241+
or
242+
n instanceof DictNode and cls = ObjectInternal::builtin("dict")
243+
or
244+
n instanceof SetNode and cls = ObjectInternal::builtin("set")
245+
or
246+
n.getNode() instanceof ImaginaryLiteral and cls = ObjectInternal::builtin("complex")
247+
or
248+
n.getNode() instanceof ListComp and cls = ObjectInternal::builtin("list")
249+
or
250+
n.getNode() instanceof SetComp and cls = ObjectInternal::builtin("set")
251+
or
252+
n.getNode() instanceof DictComp and cls = ObjectInternal::builtin("dict")
253+
}
254+
232255
predicate literal_instantiation(ControlFlowNode n, ClassObjectInternal cls, PointsToContext context) {
233256
context.appliesTo(n) and
234-
(
235-
n instanceof ListNode and cls = ObjectInternal::builtin("list")
236-
or
237-
n instanceof DictNode and cls = ObjectInternal::builtin("dict")
238-
or
239-
n instanceof SetNode and cls = ObjectInternal::builtin("set")
240-
or
241-
n.getNode() instanceof ImaginaryLiteral and cls = ObjectInternal::builtin("complex")
242-
or
243-
n.getNode() instanceof ListComp and cls = ObjectInternal::builtin("list")
244-
or
245-
n.getNode() instanceof SetComp and cls = ObjectInternal::builtin("set")
246-
or
247-
n.getNode() instanceof DictComp and cls = ObjectInternal::builtin("dict")
248-
)
257+
literal_node_class(n, cls)
249258
}
250259

251260
predicate super_instantiation(

0 commit comments

Comments
 (0)