Skip to content

Commit 093eb71

Browse files
committed
Python: ObjectAPI to ValueAPI: ReturnValueIgnored: Adds getAReturnType
1 parent 42388b0 commit 093eb71

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,9 @@ abstract class FunctionValue extends CallableValue {
517517

518518
/** Gets a class that this function may return */
519519
ClassValue getAnInferredReturnType() {
520-
//result = this.(BuiltinCallable).getAReturnType()
521-
result = this.getScope().getAReturnValueFlowNode().pointsTo().getClass()
520+
result = this.(BuiltinFunctionValue).getAReturnType()
521+
or
522+
result = this.(BuiltinMethodValue).getAReturnType()
522523
}
523524
}
524525

@@ -575,6 +576,29 @@ class BuiltinFunctionValue extends FunctionValue {
575576
override int maxParameters() {
576577
none()
577578
}
579+
580+
ClassValue getAReturnType() {
581+
/* Enumerate the types of a few builtin functions, that the CPython analysis misses.
582+
*/
583+
this = TBuiltinFunctionObject(Builtin::builtin("hex")) and result = ClassValue::str()
584+
or
585+
this = TBuiltinFunctionObject(Builtin::builtin("oct")) and result = ClassValue::str()
586+
or
587+
this = TBuiltinFunctionObject(Builtin::builtin("intern")) and result = ClassValue::str()
588+
or
589+
/* Fix a few minor inaccuracies in the CPython analysis */
590+
exists(Builtin mthd, Builtin cls | this = TBuiltinFunctionObject(mthd) and result = TBuiltinClassObject(cls)
591+
| ext_rettype(mthd, cls)) and
592+
not (
593+
this = TBuiltinFunctionObject(Builtin::builtin("__import__")) and result = ClassValue::nonetype()
594+
or
595+
this = TBuiltinFunctionObject(Builtin::builtin("compile")) and result = ClassValue::nonetype()
596+
or
597+
this = TBuiltinFunctionObject(Builtin::builtin("sum"))
598+
or
599+
this = TBuiltinFunctionObject(Builtin::builtin("filter"))
600+
)
601+
}
578602
}
579603

580604
/** Class representing builtin methods, such as `list.append` or `set.add` */
@@ -600,6 +624,15 @@ class BuiltinMethodValue extends FunctionValue {
600624
none()
601625
}
602626

627+
ClassValue getAReturnType() {
628+
exists(Builtin mthd, Builtin cls |
629+
this = TBuiltinMethodObject(mthd) and
630+
result = TBuiltinClassObject(cls)
631+
|
632+
ext_rettype(mthd, cls)
633+
)
634+
}
635+
603636
}
604637

605638
/** A class representing sequence objects with a length and tracked items.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
| functions_test.py:159:5:159:9 | ExprStmt | Call discards return value of function $@. The result is used in 80% of calls. | functions_test.py:12:1:12:11 | Function ok2 | ok2 |
22
| functions_test.py:160:5:160:9 | ExprStmt | Call discards return value of function $@. The result is used in 80% of calls. | functions_test.py:36:1:36:11 | Function ok4 | ok4 |
3-
| functions_test.py:161:5:161:17 | ExprStmt | Call discards return value of function $@. The result is used in 80% of calls. | file://:Compiled Code:0:0:0:0 | Builtin-function sorted | sorted |
3+
| functions_test.py:161:5:161:17 | ExprStmt | Call discards return value of function $@. The result is used in 80% of calls. | file://:0:0:0:0 | Builtin-function sorted | sorted |

0 commit comments

Comments
 (0)