mirror of
https://github.com/github/codeql.git
synced 2026-04-30 11:15:13 +02:00
Merge pull request #3039 from BekaValentine/python-objectapi-to-valueapi-returnvalueignored
Python: ObjectAPI to ValueAPI: ReturnValueIgnored
This commit is contained in:
@@ -15,17 +15,18 @@
|
||||
*/
|
||||
|
||||
import python
|
||||
import semmle.python.objects.Callables
|
||||
|
||||
predicate meaningful_return_value(Expr val) {
|
||||
val instanceof Name
|
||||
or
|
||||
val instanceof BooleanLiteral
|
||||
or
|
||||
exists(FunctionObject callee |
|
||||
exists(FunctionValue callee |
|
||||
val = callee.getACall().getNode() and returns_meaningful_value(callee)
|
||||
)
|
||||
or
|
||||
not exists(FunctionObject callee | val = callee.getACall().getNode()) and not val instanceof Name
|
||||
not exists(FunctionValue callee | val = callee.getACall().getNode()) and not val instanceof Name
|
||||
}
|
||||
|
||||
/* Value is used before returning, and thus its value is not lost if ignored */
|
||||
@@ -35,10 +36,10 @@ predicate used_value(Expr val) {
|
||||
)
|
||||
}
|
||||
|
||||
predicate returns_meaningful_value(FunctionObject f) {
|
||||
not exists(f.getFunction().getFallthroughNode()) and
|
||||
predicate returns_meaningful_value(FunctionValue f) {
|
||||
not exists(f.getScope().getFallthroughNode()) and
|
||||
(
|
||||
exists(Return ret, Expr val | ret.getScope() = f.getFunction() and val = ret.getValue() |
|
||||
exists(Return ret, Expr val | ret.getScope() = f.getScope() and val = ret.getValue() |
|
||||
meaningful_return_value(val) and
|
||||
not used_value(val)
|
||||
)
|
||||
@@ -48,7 +49,9 @@ predicate returns_meaningful_value(FunctionObject f) {
|
||||
* Ignore __import__ as it is often called purely for side effects
|
||||
*/
|
||||
|
||||
f.isC() and f.getAnInferredReturnType() != theNoneType() and not f.getName() = "__import__"
|
||||
f.isBuiltin() and
|
||||
f.getAnInferredReturnType() != ClassValue::nonetype() and
|
||||
not f.getName() = "__import__"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -61,7 +64,7 @@ predicate wrapped_in_try_except(ExprStmt call) {
|
||||
)
|
||||
}
|
||||
|
||||
from ExprStmt call, FunctionObject callee, float percentage_used, int total
|
||||
from ExprStmt call, FunctionValue callee, float percentage_used, int total
|
||||
where
|
||||
call.getValue() = callee.getACall().getNode() and
|
||||
returns_meaningful_value(callee) and
|
||||
|
||||
@@ -581,6 +581,13 @@ abstract class FunctionValue extends CallableValue {
|
||||
exists(Expr expr, AstNode origin | expr.pointsTo(this, origin) | not origin instanceof Lambda)
|
||||
)
|
||||
}
|
||||
|
||||
/** Gets a class that this function may return */
|
||||
ClassValue getAnInferredReturnType() {
|
||||
result = TBuiltinClassObject(this.(BuiltinFunctionObjectInternal).getReturnType())
|
||||
or
|
||||
result = TBuiltinClassObject(this.(BuiltinMethodObjectInternal).getReturnType())
|
||||
}
|
||||
}
|
||||
|
||||
/** Class representing Python functions */
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
|
||||
Reference in New Issue
Block a user