Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5c3242

Browse files
committed
Python points-to: Handle list, dict and float literals as instances.
1 parent 48297e2 commit f5c3242

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ newtype TObject =
8080
TSpecificInstance(ControlFlowNode instantiation, ClassObjectInternal cls, PointsToContext context) {
8181
PointsTo2::points_to(instantiation.(CallNode).getFunction(), context, cls, _) and
8282
cls.isSpecial() = false
83+
or
84+
literal_instantiation(instantiation, cls, context)
8385
}
8486
or
8587
TSelfInstance(ParameterDefinition def, PointsToContext context, PythonClassObjectInternal cls) {
@@ -129,6 +131,19 @@ predicate class_method(CallNode instantiation, CallableObjectInternal function,
129131
PointsTo2::points_to(instantiation.getArg(0), context, function, _)
130132
}
131133

134+
predicate literal_instantiation(ControlFlowNode n, ClassObjectInternal cls, PointsToContext context) {
135+
context.appliesTo(n) and
136+
(
137+
n instanceof ListNode and cls = ObjectInternal::builtin("list")
138+
or
139+
n instanceof DictNode and cls = ObjectInternal::builtin("dict")
140+
or
141+
n.getNode() instanceof FloatLiteral and cls = ObjectInternal::builtin("float")
142+
or
143+
n.getNode() instanceof ImaginaryLiteral and cls = ObjectInternal::builtin("complex")
144+
)
145+
}
146+
132147
predicate super_instantiation(CallNode instantiation, ObjectInternal self, ClassObjectInternal startclass, PointsToContext context) {
133148
PointsTo2::points_to(instantiation.getFunction(), context, ObjectInternal::builtin("super"), _) and
134149
(
@@ -267,12 +282,20 @@ library class ClassDecl extends @py_object {
267282
predicate isSpecial() {
268283
exists(string name |
269284
this = Builtin::special(name) |
270-
not name = "object" and
271-
not name = "list" and
272-
not name = "set" and
273-
not name = "dict" and
274-
not name.matches("%Exception") and
275-
not name.matches("%Error")
285+
name = "type" or
286+
name = "bool" or
287+
name = "NoneType" or
288+
name = "int" or
289+
name = "long" or
290+
name = "str" or
291+
name = "bytes" or
292+
name = "unicode" or
293+
name = "tuple" or
294+
name = "property" or
295+
name = "classmethod" or
296+
name = "staticmethod" or
297+
name = "MethodType" or
298+
name = "ModuleType"
276299
)
277300
}
278301

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ cached module PointsTo2 {
166166
/* Holds if the edge `pred` -> `succ` is reachable, given the context `context`.
167167
*/
168168
pragma [noopt]
169-
private predicate controlledReachableEdge(BasicBlock pred, BasicBlock succ, PointsToContext context) {
169+
cached predicate controlledReachableEdge(BasicBlock pred, BasicBlock succ, PointsToContext context) {
170170
exists(ConditionBlock guard, ObjectInternal value, boolean sense, ControlFlowNode test |
171171
test = guard.getLastNode() and
172172
points_to(test, context, value, _) and

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ private import semmle.python.types.Builtins
66
private cached predicate is_an_object(@py_object obj) {
77
/* CFG nodes for numeric literals, all of which have a @py_cobject for the value of that literal */
88
obj instanceof ControlFlowNode and
9-
not obj.(ControlFlowNode).getNode() instanceof ImmutableLiteral
9+
not obj.(ControlFlowNode).getNode() instanceof IntegerLiteral and
10+
not obj.(ControlFlowNode).getNode() instanceof StrConst
1011
or
1112
obj instanceof Builtin
1213
}

0 commit comments

Comments
 (0)