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

Skip to content

Commit 35ada17

Browse files
committed
Python: Use object as default return type for built-ins.
1 parent 5813209 commit 35ada17

5 files changed

Lines changed: 26 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,19 @@ class BuiltinMethodObjectInternal extends CallableObjectInternal, TBuiltinMethod
333333
}
334334

335335
Builtin getReturnType() {
336+
/* If we have a record of the return type in our stubs, use that. */
336337
exists(Builtin func |
337338
func = this.getBuiltin() |
338339
ext_rettype(func, result)
339340
)
341+
or
342+
/* Otherwise, if no such record exists, use `object` as the return type. */
343+
not exists(Builtin func |
344+
// We cannot do `this.getBuiltin()` here, as that would introduce negative recursion.
345+
// Instead, we appeal directly to the underlying IPA type.
346+
this = TBuiltinMethodObject(func) and
347+
ext_rettype(func, _))
348+
and result = Builtin::builtin("object")
340349
}
341350

342351
override ControlFlowNode getOrigin() {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
| l_calls.py:64 | ControlFlowNode for d |

python/ql/test/library-tests/PointsTo/new/PointsToWithContext.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ WARNING: Predicate points_to has been deprecated and may be removed in future (P
292292
| g_class_init.py:35 | ControlFlowNode for super | builtin-class super | builtin-class type | 35 | runtime |
293293
| g_class_init.py:35 | ControlFlowNode for super() | super() | builtin-class super | 35 | runtime |
294294
| g_class_init.py:36 | ControlFlowNode for Attribute | super().__init__ | builtin-class method | 36 | runtime |
295+
| g_class_init.py:36 | ControlFlowNode for Attribute() | Attribute() | builtin-class object | 36 | runtime |
295296
| g_class_init.py:36 | ControlFlowNode for D | class D | builtin-class type | 32 | runtime |
296297
| g_class_init.py:36 | ControlFlowNode for self | self | class D | 34 | runtime |
297298
| g_class_init.py:36 | ControlFlowNode for super | builtin-class super | builtin-class type | 36 | runtime |
@@ -727,13 +728,16 @@ WARNING: Predicate points_to has been deprecated and may be removed in future (P
727728
| l_calls.py:62 | ControlFlowNode for c | Builtin-method get | builtin-class method_descriptor | 62 | import |
728729
| l_calls.py:62 | ControlFlowNode for dict | builtin-class dict | builtin-class type | 62 | import |
729730
| l_calls.py:63 | ControlFlowNode for Attribute | Builtin-method get | builtin-class method_descriptor | 63 | import |
731+
| l_calls.py:63 | ControlFlowNode for Attribute() | Attribute() | builtin-class object | 63 | import |
730732
| l_calls.py:63 | ControlFlowNode for Dict | Dict | builtin-class dict | 63 | import |
731733
| l_calls.py:63 | ControlFlowNode for IntegerLiteral | int 5 | builtin-class int | 63 | import |
732734
| l_calls.py:63 | ControlFlowNode for Str | 'foo' | builtin-class str | 63 | import |
735+
| l_calls.py:63 | ControlFlowNode for d | Attribute() | builtin-class object | 63 | import |
733736
| l_calls.py:63 | ControlFlowNode for dict | builtin-class dict | builtin-class type | 63 | import |
734737
| l_calls.py:64 | ControlFlowNode for a | Builtin-method bit_length | builtin-class method_descriptor | 58 | import |
735738
| l_calls.py:64 | ControlFlowNode for b | Attribute() | builtin-class int | 59 | import |
736739
| l_calls.py:64 | ControlFlowNode for c | Builtin-method get | builtin-class method_descriptor | 62 | import |
740+
| l_calls.py:64 | ControlFlowNode for d | Attribute() | builtin-class object | 63 | import |
737741
| m_attributes.py:3 | ControlFlowNode for C | class C | builtin-class type | 3 | import |
738742
| m_attributes.py:3 | ControlFlowNode for ClassExpr | class C | builtin-class type | 3 | import |
739743
| m_attributes.py:3 | ControlFlowNode for object | builtin-class object | builtin-class type | 3 | import |
@@ -880,6 +884,8 @@ WARNING: Predicate points_to has been deprecated and may be removed in future (P
880884
| q_super.py:3 | ControlFlowNode for __init__ | Function __init__ | builtin-class function | 3 | import |
881885
| q_super.py:4 | ControlFlowNode for Attribute | super().__init__ | builtin-class method | 4 | code/q_super.py:12 from runtime |
882886
| q_super.py:4 | ControlFlowNode for Attribute | super().__init__ | builtin-class method | 4 | runtime |
887+
| q_super.py:4 | ControlFlowNode for Attribute() | Attribute() | builtin-class object | 4 | code/q_super.py:12 from runtime |
888+
| q_super.py:4 | ControlFlowNode for Attribute() | Attribute() | builtin-class object | 4 | runtime |
883889
| q_super.py:4 | ControlFlowNode for Base2 | class Base2 | builtin-class type | 1 | code/q_super.py:12 from runtime |
884890
| q_super.py:4 | ControlFlowNode for Base2 | class Base2 | builtin-class type | 1 | runtime |
885891
| q_super.py:4 | ControlFlowNode for self | self | class Base2 | 3 | runtime |
@@ -1049,6 +1055,7 @@ WARNING: Predicate points_to has been deprecated and may be removed in future (P
10491055
| q_super.py:75 | ControlFlowNode for i | super().__init__ | builtin-class method | 75 | runtime |
10501056
| q_super.py:75 | ControlFlowNode for s | super() | builtin-class super | 74 | runtime |
10511057
| q_super.py:76 | ControlFlowNode for i | super().__init__ | builtin-class method | 75 | runtime |
1058+
| q_super.py:76 | ControlFlowNode for i() | i() | builtin-class object | 76 | runtime |
10521059
| r_regressions.py:5 | ControlFlowNode for ClassExpr | class Queue | builtin-class type | 5 | import |
10531060
| r_regressions.py:5 | ControlFlowNode for Queue | class Queue | builtin-class type | 5 | import |
10541061
| r_regressions.py:5 | ControlFlowNode for object | builtin-class object | builtin-class type | 5 | import |

python/ql/test/library-tests/PointsTo/new/PointsToWithType.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ WARNING: Predicate points_to has been deprecated and may be removed in future (P
405405
| g_class_init.py:35 | ControlFlowNode for super | builtin-class super | builtin-class type | 35 |
406406
| g_class_init.py:35 | ControlFlowNode for super() | super() | builtin-class super | 35 |
407407
| g_class_init.py:36 | ControlFlowNode for Attribute | super().__init__ | builtin-class method | 36 |
408+
| g_class_init.py:36 | ControlFlowNode for Attribute() | Attribute() | builtin-class object | 36 |
408409
| g_class_init.py:36 | ControlFlowNode for D | class D | builtin-class type | 32 |
409410
| g_class_init.py:36 | ControlFlowNode for self | self | class D | 34 |
410411
| g_class_init.py:36 | ControlFlowNode for super | builtin-class super | builtin-class type | 36 |
@@ -800,13 +801,16 @@ WARNING: Predicate points_to has been deprecated and may be removed in future (P
800801
| l_calls.py:62 | ControlFlowNode for c | Builtin-method get | builtin-class method_descriptor | 62 |
801802
| l_calls.py:62 | ControlFlowNode for dict | builtin-class dict | builtin-class type | 62 |
802803
| l_calls.py:63 | ControlFlowNode for Attribute | Builtin-method get | builtin-class method_descriptor | 63 |
804+
| l_calls.py:63 | ControlFlowNode for Attribute() | Attribute() | builtin-class object | 63 |
803805
| l_calls.py:63 | ControlFlowNode for Dict | Dict | builtin-class dict | 63 |
804806
| l_calls.py:63 | ControlFlowNode for IntegerLiteral | int 5 | builtin-class int | 63 |
805807
| l_calls.py:63 | ControlFlowNode for Str | 'foo' | builtin-class str | 63 |
808+
| l_calls.py:63 | ControlFlowNode for d | Attribute() | builtin-class object | 63 |
806809
| l_calls.py:63 | ControlFlowNode for dict | builtin-class dict | builtin-class type | 63 |
807810
| l_calls.py:64 | ControlFlowNode for a | Builtin-method bit_length | builtin-class method_descriptor | 58 |
808811
| l_calls.py:64 | ControlFlowNode for b | Attribute() | builtin-class int | 59 |
809812
| l_calls.py:64 | ControlFlowNode for c | Builtin-method get | builtin-class method_descriptor | 62 |
813+
| l_calls.py:64 | ControlFlowNode for d | Attribute() | builtin-class object | 63 |
810814
| s_scopes.py:4 | ControlFlowNode for True | bool True | builtin-class bool | 4 |
811815
| s_scopes.py:4 | ControlFlowNode for float | bool True | builtin-class bool | 4 |
812816
| s_scopes.py:7 | ControlFlowNode for C2 | class C2 | builtin-class type | 7 |

python/ql/test/library-tests/PointsTo/new/Values.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
| g_class_init.py:35 | ControlFlowNode for super | runtime | builtin-class super | builtin-class type |
233233
| g_class_init.py:35 | ControlFlowNode for super() | runtime | super(class D, self instance of D) | builtin-class super |
234234
| g_class_init.py:36 | ControlFlowNode for Attribute | runtime | Method(builtin method __init__, self instance of D) | builtin-class method |
235+
| g_class_init.py:36 | ControlFlowNode for Attribute() | runtime | instance of object | builtin-class object |
235236
| g_class_init.py:36 | ControlFlowNode for D | runtime | class D | builtin-class type |
236237
| g_class_init.py:36 | ControlFlowNode for self | runtime | self instance of D | class D |
237238
| g_class_init.py:36 | ControlFlowNode for super | runtime | builtin-class super | builtin-class type |
@@ -584,13 +585,15 @@
584585
| l_calls.py:62 | ControlFlowNode for Attribute | import | builtin method get | builtin-class method_descriptor |
585586
| l_calls.py:62 | ControlFlowNode for dict | import | builtin-class dict | builtin-class type |
586587
| l_calls.py:63 | ControlFlowNode for Attribute | import | builtin method get | builtin-class method_descriptor |
588+
| l_calls.py:63 | ControlFlowNode for Attribute() | import | instance of object | builtin-class object |
587589
| l_calls.py:63 | ControlFlowNode for Dict | import | Dict | builtin-class dict |
588590
| l_calls.py:63 | ControlFlowNode for IntegerLiteral | import | int 5 | builtin-class int |
589591
| l_calls.py:63 | ControlFlowNode for Str | import | 'foo' | builtin-class str |
590592
| l_calls.py:63 | ControlFlowNode for dict | import | builtin-class dict | builtin-class type |
591593
| l_calls.py:64 | ControlFlowNode for a | import | builtin method bit_length | builtin-class method_descriptor |
592594
| l_calls.py:64 | ControlFlowNode for b | import | instance of int | builtin-class int |
593595
| l_calls.py:64 | ControlFlowNode for c | import | builtin method get | builtin-class method_descriptor |
596+
| l_calls.py:64 | ControlFlowNode for d | import | instance of object | builtin-class object |
594597
| m_attributes.py:3 | ControlFlowNode for ClassExpr | import | class C | builtin-class type |
595598
| m_attributes.py:3 | ControlFlowNode for object | import | builtin-class object | builtin-class type |
596599
| m_attributes.py:5 | ControlFlowNode for FunctionExpr | import | Function C.__init__ | builtin-class function |
@@ -702,6 +705,8 @@
702705
| q_super.py:3 | ControlFlowNode for FunctionExpr | import | Function Base2.__init__ | builtin-class function |
703706
| q_super.py:4 | ControlFlowNode for Attribute | code/q_super.py:12 from runtime | Method(builtin method __init__, self instance of Derived4) | builtin-class method |
704707
| q_super.py:4 | ControlFlowNode for Attribute | runtime | Method(builtin method __init__, self instance of Base2) | builtin-class method |
708+
| q_super.py:4 | ControlFlowNode for Attribute() | code/q_super.py:12 from runtime | instance of object | builtin-class object |
709+
| q_super.py:4 | ControlFlowNode for Attribute() | runtime | instance of object | builtin-class object |
705710
| q_super.py:4 | ControlFlowNode for Base2 | code/q_super.py:12 from runtime | class Base2 | builtin-class type |
706711
| q_super.py:4 | ControlFlowNode for Base2 | runtime | class Base2 | builtin-class type |
707712
| q_super.py:4 | ControlFlowNode for self | code/q_super.py:12 from runtime | self instance of Derived4 | class Derived4 |
@@ -842,6 +847,7 @@
842847
| q_super.py:75 | ControlFlowNode for Attribute | runtime | Method(builtin method __init__, self instance of M) | builtin-class method |
843848
| q_super.py:75 | ControlFlowNode for s | runtime | super(class M, self instance of M) | builtin-class super |
844849
| q_super.py:76 | ControlFlowNode for i | runtime | Method(builtin method __init__, self instance of M) | builtin-class method |
850+
| q_super.py:76 | ControlFlowNode for i() | runtime | instance of object | builtin-class object |
845851
| r_regressions.py:5 | ControlFlowNode for ClassExpr | import | class Queue | builtin-class type |
846852
| r_regressions.py:5 | ControlFlowNode for object | import | builtin-class object | builtin-class type |
847853
| r_regressions.py:7 | ControlFlowNode for FunctionExpr | import | Function Queue.__init__ | builtin-class function |

0 commit comments

Comments
 (0)