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

Skip to content

Commit 7dd4030

Browse files
committed
Pattern cases: support type-flow
1 parent a06ac42 commit 7dd4030

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

java/ql/lib/semmle/code/java/dataflow/TypeFlow.qll

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,21 @@ predicate arrayInstanceOfGuarded(ArrayAccess aa, RefType t) {
440440
)
441441
}
442442

443+
/**
444+
* Holds if `va` is an access to a value that is guarded by `case T t`.
445+
*/
446+
private predicate patternCaseGuarded(VarAccess va, RefType t) {
447+
exists(PatternCase pc, BaseSsaVariable v |
448+
va = v.getAUse() and
449+
(
450+
pc.getSwitch().getExpr() = v.getAUse() or
451+
pc.getSwitchExpr().getExpr() = v.getAUse()
452+
) and
453+
pc.getDecl().getBasicBlock().bbDominates(va.getBasicBlock()) and
454+
t = pc.getDecl().getType()
455+
)
456+
}
457+
443458
/**
444459
* Holds if `t` is the type of the `this` value corresponding to the the
445460
* `SuperAccess`. As the `SuperAccess` expression has the type of the supertype,
@@ -465,7 +480,8 @@ private predicate typeFlowBaseCand(TypeFlowNode n, RefType t) {
465480
instanceOfGuarded(n.asExpr(), srctype) or
466481
arrayInstanceOfGuarded(n.asExpr(), srctype) or
467482
n.asExpr().(FunctionalExpr).getConstructedType() = srctype or
468-
superAccess(n.asExpr(), srctype)
483+
superAccess(n.asExpr(), srctype) or
484+
patternCaseGuarded(n.asExpr(), srctype)
469485
|
470486
t = srctype.(BoundedType).getAnUltimateUpperBoundType()
471487
or

java/ql/test/library-tests/typeflow/UnionTypes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ private void next(Sup x) {
4444
if (x instanceof Inter) {
4545
x.hashCode();
4646
}
47+
var hashCode = switch (x) {
48+
case Inter i -> x.hashCode();
49+
default -> 0;
50+
};
4751
}
4852

4953
void m3(Object d) {

java/ql/test/library-tests/typeflow/typeflow.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
| A.java:70:23:70:24 | x2 | Integer | false |
1515
| A.java:92:18:92:18 | n | Integer | false |
1616
| UnionTypes.java:45:7:45:7 | x | Inter | false |
17+
| UnionTypes.java:48:23:48:23 | x | Inter | false |

java/ql/test/library-tests/typeflow/uniontypeflow.expected

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
| UnionTypes.java:44:9:44:9 | x | 3 | A3 | false |
2020
| UnionTypes.java:45:7:45:7 | x | 2 | A1 | false |
2121
| UnionTypes.java:45:7:45:7 | x | 2 | A2 | true |
22-
| UnionTypes.java:51:7:51:7 | d | 3 | A1 | false |
23-
| UnionTypes.java:51:7:51:7 | d | 3 | A2 | false |
24-
| UnionTypes.java:51:7:51:7 | d | 3 | A3 | false |
22+
| UnionTypes.java:47:28:47:28 | x | 3 | A1 | false |
23+
| UnionTypes.java:47:28:47:28 | x | 3 | A2 | true |
24+
| UnionTypes.java:47:28:47:28 | x | 3 | A3 | false |
25+
| UnionTypes.java:48:23:48:23 | x | 2 | A1 | false |
26+
| UnionTypes.java:48:23:48:23 | x | 2 | A2 | true |
27+
| UnionTypes.java:55:7:55:7 | d | 3 | A1 | false |
28+
| UnionTypes.java:55:7:55:7 | d | 3 | A2 | false |
29+
| UnionTypes.java:55:7:55:7 | d | 3 | A3 | false |

0 commit comments

Comments
 (0)