Skip to content

Commit e142591

Browse files
authored
Merge pull request #658 from calumgrant/cs/extractor/for-is
C#: Fix extraction bug for variable declarations in for condition
2 parents 6648c84 + 5f269b2 commit e142591

File tree

7 files changed

+38
-5
lines changed

7 files changed

+38
-5
lines changed

change-notes/1.20/analysis-csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
## Changes to code extraction
1919

20+
* Fix extraction of `for` statements where the condition declares new variables using `is`.
2021
* Initializers of `stackalloc` arrays are now extracted.
2122

2223
## Changes to QL libraries

csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/For.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ protected override void Populate()
2828
Expression.Create(cx, init, this, child--);
2929
}
3030

31-
Statement.Create(cx, Stmt.Statement, this, 1 + Stmt.Incrementors.Count);
31+
if (Stmt.Condition != null)
32+
{
33+
Expression.Create(cx, Stmt.Condition, this, 0);
34+
}
3235

3336
child = 1;
3437
foreach (var inc in Stmt.Incrementors)
3538
{
3639
Expression.Create(cx, inc, this, child++);
3740
}
3841

39-
if (Stmt.Condition != null)
40-
{
41-
Expression.Create(cx, Stmt.Condition, this, 0);
42-
}
42+
Statement.Create(cx, Stmt.Statement, this, 1 + Stmt.Incrementors.Count);
4343
}
4444
}
4545
}

csharp/ql/test/library-tests/csharp7/CSharp7.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,14 @@ void Test()
290290
foreach (var (a, b) in list) { }
291291
}
292292
}
293+
294+
class ForLoops
295+
{
296+
void Test()
297+
{
298+
for(int x=0; x<10 && x is int y; ++x)
299+
{
300+
Console.WriteLine(y);
301+
}
302+
}
303+
}

csharp/ql/test/library-tests/csharp7/DefUse.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@
6666
| CSharp7.cs:284:13:284:62 | IEnumerable<(Int32,String)> list = ... | CSharp7.cs:290:32:290:35 | access to local variable list |
6767
| CSharp7.cs:284:32:284:35 | item | CSharp7.cs:284:41:284:44 | access to parameter item |
6868
| CSharp7.cs:284:32:284:35 | item | CSharp7.cs:284:51:284:54 | access to parameter item |
69+
| CSharp7.cs:298:17:298:19 | Int32 x = ... | CSharp7.cs:298:22:298:22 | access to local variable x |
70+
| CSharp7.cs:298:17:298:19 | Int32 x = ... | CSharp7.cs:298:30:298:30 | access to local variable x |
71+
| CSharp7.cs:298:17:298:19 | Int32 x = ... | CSharp7.cs:298:44:298:44 | access to local variable x |
72+
| CSharp7.cs:298:35:298:39 | Int32 y | CSharp7.cs:300:31:300:31 | access to local variable y |
73+
| CSharp7.cs:298:42:298:44 | ++... | CSharp7.cs:298:22:298:22 | access to local variable x |
74+
| CSharp7.cs:298:42:298:44 | ++... | CSharp7.cs:298:30:298:30 | access to local variable x |
75+
| CSharp7.cs:298:42:298:44 | ++... | CSharp7.cs:298:44:298:44 | access to local variable x |

csharp/ql/test/library-tests/csharp7/IsPatterns.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| CSharp7.cs:238:18:238:31 | ... is ... | CSharp7.cs:238:23:238:28 | access to type String | String | CSharp7.cs:238:23:238:31 | String s1 | false |
33
| CSharp7.cs:245:18:245:28 | ... is ... | CSharp7.cs:245:23:245:25 | access to type Object | Object | CSharp7.cs:245:23:245:28 | Object v1 | true |
44
| CSharp7.cs:255:27:255:40 | ... is ... | CSharp7.cs:255:32:255:37 | access to type String | String | CSharp7.cs:255:32:255:40 | String s4 | false |
5+
| CSharp7.cs:298:30:298:39 | ... is ... | CSharp7.cs:298:35:298:37 | access to type Int32 | Int32 | CSharp7.cs:298:35:298:39 | Int32 y | false |

csharp/ql/test/library-tests/csharp7/LocalTaintFlow.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,14 @@
193193
| CSharp7.cs:288:36:288:39 | access to local variable list | CSharp7.cs:290:32:290:35 | access to local variable list |
194194
| CSharp7.cs:290:23:290:23 | Int32 a | CSharp7.cs:290:18:290:27 | (..., ...) |
195195
| CSharp7.cs:290:26:290:26 | String b | CSharp7.cs:290:18:290:27 | (..., ...) |
196+
| CSharp7.cs:298:17:298:19 | SSA def(x) | CSharp7.cs:298:22:298:39 | SSA phi(x) |
197+
| CSharp7.cs:298:19:298:19 | 0 | CSharp7.cs:298:17:298:19 | SSA def(x) |
198+
| CSharp7.cs:298:22:298:22 | access to local variable x | CSharp7.cs:298:22:298:25 | ... < ... |
199+
| CSharp7.cs:298:22:298:22 | access to local variable x | CSharp7.cs:298:30:298:30 | access to local variable x |
200+
| CSharp7.cs:298:22:298:25 | ... < ... | CSharp7.cs:298:22:298:39 | ... && ... |
201+
| CSharp7.cs:298:22:298:39 | SSA phi(x) | CSharp7.cs:298:22:298:22 | access to local variable x |
202+
| CSharp7.cs:298:30:298:30 | access to local variable x | CSharp7.cs:298:35:298:39 | SSA def(y) |
203+
| CSharp7.cs:298:30:298:30 | access to local variable x | CSharp7.cs:298:44:298:44 | access to local variable x |
204+
| CSharp7.cs:298:30:298:39 | ... is ... | CSharp7.cs:298:22:298:39 | ... && ... |
205+
| CSharp7.cs:298:35:298:39 | SSA def(y) | CSharp7.cs:300:31:300:31 | access to local variable y |
206+
| CSharp7.cs:298:42:298:44 | SSA def(x) | CSharp7.cs:298:22:298:39 | SSA phi(x) |

csharp/ql/test/library-tests/csharp7/LocalVariables.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@
6565
| CSharp7.cs:288:30:288:30 | b | string |
6666
| CSharp7.cs:290:23:290:23 | a | int |
6767
| CSharp7.cs:290:26:290:26 | b | string |
68+
| CSharp7.cs:298:17:298:17 | x | int |
69+
| CSharp7.cs:298:39:298:39 | y | int |

0 commit comments

Comments
 (0)