From d9152392ce7be7d0ca7c0415b464abf470e607c3 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 2 Jun 2026 16:05:03 +0200 Subject: [PATCH] C#: Add test case and expected output. --- csharp/ql/test/library-tests/spans/Slice.cs | 19 ++++++++++++++++ .../test/library-tests/spans/slice.expected | 22 +++++++++++++++++++ csharp/ql/test/library-tests/spans/slice.ql | 18 +++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 csharp/ql/test/library-tests/spans/Slice.cs create mode 100644 csharp/ql/test/library-tests/spans/slice.expected create mode 100644 csharp/ql/test/library-tests/spans/slice.ql diff --git a/csharp/ql/test/library-tests/spans/Slice.cs b/csharp/ql/test/library-tests/spans/Slice.cs new file mode 100644 index 00000000000..882cdf59917 --- /dev/null +++ b/csharp/ql/test/library-tests/spans/Slice.cs @@ -0,0 +1,19 @@ +using System; + +public class C +{ + public void M(int a) + { + var s = "hello world"; + var sub1 = s[1..a]; + var sub2 = s[..2]; + var sub3 = s[3..]; + var sub4 = s[..^4]; + + Span sp = null; + var slice1 = sp[5..a]; + var slice2 = sp[..6]; + var slice3 = sp[7..]; + var slice4 = sp[..^8]; + } +} diff --git a/csharp/ql/test/library-tests/spans/slice.expected b/csharp/ql/test/library-tests/spans/slice.expected new file mode 100644 index 00000000000..feadf14a78d --- /dev/null +++ b/csharp/ql/test/library-tests/spans/slice.expected @@ -0,0 +1,22 @@ +methodCalls +| Slice.cs:8:20:8:26 | call to method Substring | Substring(int, int) | 0 | 1 | +| Slice.cs:8:20:8:26 | call to method Substring | Substring(int, int) | 1 | access to parameter a - 1 | +| Slice.cs:9:20:9:25 | call to method Substring | Substring(int, int) | 0 | 0 | +| Slice.cs:9:20:9:25 | call to method Substring | Substring(int, int) | 1 | 2 | +| Slice.cs:10:20:10:25 | call to method Substring | Substring(int, int) | 0 | 3 | +| Slice.cs:10:20:10:25 | call to method Substring | Substring(int, int) | 1 | access to property Length - 3 | +| Slice.cs:11:20:11:26 | call to method Substring | Substring(int, int) | 0 | 0 | +| Slice.cs:11:20:11:26 | call to method Substring | Substring(int, int) | 1 | access to property Length - 4 | +| Slice.cs:14:22:14:29 | call to method Slice | Slice(int, int) | 0 | 5 | +| Slice.cs:14:22:14:29 | call to method Slice | Slice(int, int) | 1 | access to parameter a - 5 | +| Slice.cs:15:22:15:28 | call to method Slice | Slice(int, int) | 0 | 0 | +| Slice.cs:15:22:15:28 | call to method Slice | Slice(int, int) | 1 | 6 | +| Slice.cs:16:22:16:28 | call to method Slice | Slice(int, int) | 0 | 7 | +| Slice.cs:16:22:16:28 | call to method Slice | Slice(int, int) | 1 | access to property Length - 7 | +| Slice.cs:17:22:17:29 | call to method Slice | Slice(int, int) | 0 | 0 | +| Slice.cs:17:22:17:29 | call to method Slice | Slice(int, int) | 1 | access to property Length - 8 | +propertyCalls +| Slice.cs:10:20:10:25 | access to property Length | Slice.cs:10:20:10:20 | access to local variable s | +| Slice.cs:11:20:11:26 | access to property Length | Slice.cs:11:20:11:20 | access to local variable s | +| Slice.cs:16:22:16:28 | access to property Length | Slice.cs:16:22:16:23 | access to local variable sp | +| Slice.cs:17:22:17:29 | access to property Length | Slice.cs:17:22:17:23 | access to local variable sp | diff --git a/csharp/ql/test/library-tests/spans/slice.ql b/csharp/ql/test/library-tests/spans/slice.ql new file mode 100644 index 00000000000..8cddc4a875a --- /dev/null +++ b/csharp/ql/test/library-tests/spans/slice.ql @@ -0,0 +1,18 @@ +import csharp + +private string printExpr(Expr e) { + e = + any(SubExpr sub | + result = sub.getLeftOperand().toString() + " - " + sub.getRightOperand().toString() + ) + or + not e instanceof SubExpr and + result = e.toString() +} + +query predicate methodCalls(MethodCall mc, string m, int i, string arg) { + m = mc.getTarget().toStringWithTypes() and + arg = printExpr(mc.getArgument(i)) +} + +query predicate propertyCalls(PropertyCall p, Expr qualifier) { qualifier = p.getQualifier() }