From 6a1203a60f1a31aabfb5149d4b585c734aa923d9 Mon Sep 17 00:00:00 2001 From: Rebecca Valentine Date: Mon, 9 Mar 2020 12:52:15 -0700 Subject: [PATCH] Python: Adds modernized predicates --- .../ql/src/semmle/python/types/Exceptions.qll | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/python/ql/src/semmle/python/types/Exceptions.qll b/python/ql/src/semmle/python/types/Exceptions.qll index 214cb461b7b..ecb36e859cb 100644 --- a/python/ql/src/semmle/python/types/Exceptions.qll +++ b/python/ql/src/semmle/python/types/Exceptions.qll @@ -269,6 +269,18 @@ class ExceptFlowNode extends ControlFlowNode { ) } + private predicate handledObject(Value val, ClassValue cls, ControlFlowNode origin) { + val.getClass() = cls and + ( + this.getType().pointsTo(val, origin) + or + exists(TupleValue tup | + this.handledObject(tup, ClassValue::tuple(), _) | + val = tup.getItem(_) and origin.pointsTo(val) + ) + ) + } + /** Gets the inferred type(s) that are handled by this node, splitting tuples if possible. */ pragma [noinline] predicate handledException_objectapi(Object obj, ClassObject cls, ControlFlowNode origin) { @@ -278,6 +290,15 @@ class ExceptFlowNode extends ControlFlowNode { origin = this } + /** Gets the inferred type(s) that are handled by this node, splitting tuples if possible. */ + pragma [noinline] + predicate handledException(Value val, ClassValue cls, ControlFlowNode origin) { + this.handledObject(val, cls, origin) and not cls = ClassValue::tuple() + or + not exists(this.getNode().(ExceptStmt).getType()) and val = ClassValue::baseException() and cls = ClassValue::type() and + origin = this + } + /** Whether this `except` handles `cls` */ predicate handles(ClassObject cls) { exists(ClassObject handled |