Skip to content

Commit 185700a

Browse files
authored
Merge pull request #437 from calumgrant/cs/in-parameters
C#: Correctly handle `in` arguments
2 parents fb19084 + 9f04ace commit 185700a

File tree

12 files changed

+38
-11
lines changed

12 files changed

+38
-11
lines changed

change-notes/1.19/analysis-csharp.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## General improvements
44

55
* Control flow graph improvements:
6-
* The control flow graph construction now takes simple Boolean conditions on local scope variables into account. For example, in `if (b) x = 0; if (b) x = 1;`, the control flow graph will reflect that taking the `true` (resp. `false`) branch in the first condition implies taking the same branch in the second condition. In effect, the first assignment to `x` will now be identified as being dead.
6+
* The control flow graph construction now takes simple Boolean conditions on local scope variables into account. For example, in `if (b) x = 0; if (b) x = 1;`, the control flow graph will reflect that taking the `true` (resp. `false`) branch in the first condition implies taking the same branch in the second condition. In effect, the first assignment to `x` will now be identified as being dead.
77
* Code that is only reachable from a constant failing assertion, such as `Debug.Assert(false)`, is considered to be unreachable.
88

99
## New queries
@@ -20,7 +20,11 @@
2020
| Cross-site scripting (`cs/web/xss`) | More results | This query now finds cross-site scripting vulnerabilities in ASP.NET Core applications. |
2121
| *@name of query (Query ID)*| *Impact on results* | *How/why the query has changed* |
2222

23+
## Changes to code extraction
24+
25+
* Arguments passed using `in` are now extracted.
2326

2427
## Changes to QL libraries
2528

2629
* `getArgument()` on `AccessorCall` has been improved so it now takes tuple assignments into account. For example, the argument for the implicit `value` parameter in the setter of property `P` is `0` in `(P, x) = (0, 1)`. Additionally, the argument for the `value` parameter in compound assignments is now only the expanded value, for example, in `P += 7` the argument is `P + 7` and not `7`.
30+
* The predicate `isInArgument()` has been added to the `AssignableAccess` class. This holds for expressions that are passed as arguments using `in`.

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ private void PopulateArgument(ArgumentSyntax arg, int child)
223223
case SyntaxKind.None:
224224
mode = 0;
225225
break;
226+
case SyntaxKind.InKeyword:
227+
mode = 3;
228+
break;
226229
default:
227230
throw new InternalError(arg, "Unknown argument type");
228231
}

csharp/ql/src/semmle/code/csharp/exprs/Access.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ class AssignableAccess extends Access, @assignable_access_expr {
174174
isOutArgument() or
175175
isRefArgument()
176176
}
177+
178+
/**
179+
* Holds if this access passes the assignable being accessed as an `in`
180+
* argument in a method call.
181+
*/
182+
predicate isInArgument() {
183+
expr_argument(this, 3)
184+
}
177185
}
178186

179187
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| csharp72.cs:18:12:18:12 | access to local variable s |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import csharp
2+
3+
from AssignableAccess e
4+
where e.isInArgument()
5+
select e
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| csharp72.cs:42:23:42:34 | 85 |
2-
| csharp72.cs:47:31:47:31 | 1 |
1+
| csharp72.cs:48:23:48:34 | 85 |
2+
| csharp72.cs:53:31:53:31 | 1 |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| csharp72.cs:47:27:47:27 | X |
2-
| csharp72.cs:49:28:49:28 | F |
1+
| csharp72.cs:53:27:53:27 | X |
2+
| csharp72.cs:55:28:55:28 | F |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| csharp72.cs:28:17:28:30 | ReadonlyStruct |
2-
| csharp72.cs:36:21:36:37 | ReadonlyRefStruct |
1+
| csharp72.cs:34:17:34:30 | ReadonlyStruct |
2+
| csharp72.cs:42:21:42:37 | ReadonlyRefStruct |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| csharp72.cs:25:31:25:33 | Del |
1+
| csharp72.cs:31:31:31:33 | Del |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| csharp72.cs:20:22:20:22 | F |
1+
| csharp72.cs:26:22:26:22 | F |

0 commit comments

Comments
 (0)