Skip to content

Commit 425f0fa

Browse files
committed
C#: Split pattern reads into multiple steps steps.
1 parent 2a5c794 commit 425f0fa

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

+38-19
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,24 @@ module LocalFlow {
610610
or
611611
exists(Switch sw | sw.getACase() = cfe and sw.getExpr() = e1 and scope = sw)
612612
)
613+
or
614+
isSuccessor = true and
615+
scope =
616+
any(IsExpr ie |
617+
e1 = ie.getExpr() and
618+
e2 = ie.getPattern()
619+
)
620+
or
621+
isSuccessor = true and
622+
scope =
623+
any(Switch e |
624+
e1 = e.getExpr() and
625+
e2 = e.getACase().getPattern()
626+
)
627+
or
628+
isSuccessor = false and
629+
e2 = e1.(RecursivePatternExpr).getPropertyPatterns() and
630+
scope = e1
613631
)
614632
}
615633

@@ -907,27 +925,28 @@ private predicate fieldOrPropertyStore(Expr e, ContentSet c, Expr src, Expr q, b
907925
)
908926
}
909927

910-
private predicate patternPropertyRead0(RecursivePatternExpr rpe, ContentSet c, VariablePatternExpr e) {
911-
exists(TypeAccess ta, Property prop |
912-
ta = rpe.getTypeAccess() and
913-
e = rpe.getPropertyPatterns().getPattern(_) and
914-
prop.getDeclaringType() = ta.getType() and
915-
prop.getName() = e.(LabeledPatternExpr).getLabel() and
916-
c.isProperty(prop)
928+
/**
929+
* TODO: Should we consider to override getType on pattern expressions?
930+
*/
931+
private Type getPatternType(PatternExpr pe) {
932+
result = pe.(RecursivePatternExpr).getTypeAccess().getType()
933+
or
934+
not pe instanceof LabeledPatternExpr and
935+
result = getPatternType(pe.getParent())
936+
or
937+
exists(Property p |
938+
result = p.getType() and
939+
p.getDeclaringType() = getPatternType(pe.getParent()) and
940+
p.getName() = pe.(LabeledPatternExpr).getLabel()
917941
)
918942
}
919943

920-
private predicate patternPropertyRead(Expr e1, ContentSet c, VariablePatternExpr e2) {
921-
exists(IsExpr ie, RecursivePatternExpr rpe |
922-
e1 = ie.getExpr() and
923-
rpe = ie.getPattern() and
924-
patternPropertyRead0(rpe, c, e2)
925-
)
926-
or
927-
exists(Switch sw, RecursivePatternExpr rpe |
928-
e1 = sw.getExpr() and
929-
rpe = sw.getACase().getPattern() and
930-
patternPropertyRead0(rpe, c, e2)
944+
private predicate patternPropertyRead(PropertyPatternExpr pe, ContentSet c, LabeledPatternExpr e) {
945+
exists(Property prop |
946+
e = pe.getPattern(_) and
947+
prop.getDeclaringType() = getPatternType(pe) and
948+
prop.getName() = e.getLabel() and
949+
c.isProperty(prop)
931950
)
932951
}
933952

@@ -2454,7 +2473,7 @@ private class ReadStepConfiguration extends ControlFlowReachabilityConfiguration
24542473
isSuccessor = false
24552474
or
24562475
exactScope = false and
2457-
isSuccessor = true and
2476+
isSuccessor = false and
24582477
patternPropertyRead(e1, _, e2) and
24592478
scope = e1
24602479
}

0 commit comments

Comments
 (0)