From ade119f4a8b93b275a1ae879fec48ee2044eeb3e Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Wed, 2 Feb 2022 11:25:34 +0100 Subject: [PATCH] C#: Add flow test cases for undetected value flow, when making variable bindinds in pattern matching. --- .../dataflow/patterns/PatternFlow.expected | 5 ++ .../dataflow/patterns/PatternFlow.ql | 11 ++++ .../dataflow/patterns/Patterns.cs | 54 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 csharp/ql/test/library-tests/dataflow/patterns/PatternFlow.expected create mode 100644 csharp/ql/test/library-tests/dataflow/patterns/PatternFlow.ql create mode 100644 csharp/ql/test/library-tests/dataflow/patterns/Patterns.cs diff --git a/csharp/ql/test/library-tests/dataflow/patterns/PatternFlow.expected b/csharp/ql/test/library-tests/dataflow/patterns/PatternFlow.expected new file mode 100644 index 000000000000..f965891244cb --- /dev/null +++ b/csharp/ql/test/library-tests/dataflow/patterns/PatternFlow.expected @@ -0,0 +1,5 @@ +failures +edges +nodes +subpaths +#select diff --git a/csharp/ql/test/library-tests/dataflow/patterns/PatternFlow.ql b/csharp/ql/test/library-tests/dataflow/patterns/PatternFlow.ql new file mode 100644 index 000000000000..55578cf970c2 --- /dev/null +++ b/csharp/ql/test/library-tests/dataflow/patterns/PatternFlow.ql @@ -0,0 +1,11 @@ +/** + * @kind path-problem + */ + +import csharp +import DataFlow::PathGraph +import TestUtilities.InlineFlowTest + +from DataFlow::PathNode source, DataFlow::PathNode sink, DefaultValueFlowConf conf +where conf.hasFlowPath(source, sink) +select sink, source, sink, "$@", source, source.toString() diff --git a/csharp/ql/test/library-tests/dataflow/patterns/Patterns.cs b/csharp/ql/test/library-tests/dataflow/patterns/Patterns.cs new file mode 100644 index 000000000000..37728dd40395 --- /dev/null +++ b/csharp/ql/test/library-tests/dataflow/patterns/Patterns.cs @@ -0,0 +1,54 @@ +using System; + +public record class RecordClass2(object Prop) { } + +public record class Nested(RecordClass2 Record) { } + +public class K +{ + private void M1() + { + var o = Source(1); + var r = new RecordClass2(o); + if (r is RecordClass2 { Prop: object p }) + { + Sink(p); // $ MISSING: hasValueFlow=1 + } + } + + private void M2() + { + var o = Source(2); + var r = new RecordClass2(o); + switch (r) + { + case RecordClass2 { Prop: object p }: + Sink(p); // $ MISSING: hasValueFlow=2 + break; + } + } + + private void M3() + { + var o = Source(3); + var s = new Nested(new RecordClass2(o)); + if (s is Nested { Record: { Prop: object p } }) + { + Sink(p); // $ MISSING: hasValueFlow=3 + } + } + + private void M4() + { + var o = Source(4); + var s = new Nested(new RecordClass2(o)); + if (s is Nested { Record.Prop: object p }) + { + Sink(p); // $ MISSING: hasValueFlow=4 + } + } + + public static void Sink(object o) { } + + static T Source(object source) => throw null; +}