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

Skip to content

Commit fa59fc6

Browse files
committed
Python: Adds modernized predicates
1 parent 0f26734 commit fa59fc6

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@ class ExceptFlowNode extends ControlFlowNode {
268268
element_from_tuple_objectapi(tup).refersTo(obj, cls, origin)
269269
)
270270
}
271+
272+
private predicate handledObject(Value val, ClassValue cls, ControlFlowNode origin) {
273+
val.getClass() = cls and
274+
(
275+
this.getType().pointsTo(val, origin)
276+
or
277+
exists(Value tup |
278+
this.handledObject(val, ClassValue::tuple(), _) |
279+
element_from_tuple(tup).pointsTo(val, origin)
280+
)
281+
)
282+
}
271283

272284
/** Gets the inferred type(s) that are handled by this node, splitting tuples if possible. */
273285
pragma [noinline]
@@ -277,6 +289,17 @@ class ExceptFlowNode extends ControlFlowNode {
277289
not exists(this.getNode().(ExceptStmt).getType()) and obj = theBaseExceptionType() and cls = theTypeType() and
278290
origin = this
279291
}
292+
293+
/** Gets the inferred type(s) that are handled by this node, splitting tuples if possible. */
294+
pragma [noinline]
295+
predicate handledException(Value val, ClassValue cls, ControlFlowNode origin) {
296+
this.handledObject(val, cls, origin) and not cls = ClassValue::tuple()
297+
or
298+
not exists(this.getNode().(ExceptStmt).getType()) and val = ClassValue::baseException() and cls = ClassValue::type() and
299+
origin = this
300+
}
301+
302+
280303

281304
/** Whether this `except` handles `cls` */
282305
predicate handles(ClassObject cls) {
@@ -294,6 +317,12 @@ private ControlFlowNode element_from_tuple_objectapi(Object tuple) {
294317
)
295318
}
296319

320+
private ControlFlowNode element_from_tuple(Value tuple) {
321+
exists(Expr x, Tuple t |
322+
x.pointsTo(tuple,t) and result = t.getAnElt().getAFlowNode()
323+
)
324+
}
325+
297326
/** A Reraising node is the node at the end of a finally block (on the exceptional branch)
298327
* that reraises the current exception.
299328
*/

0 commit comments

Comments
 (0)