Skip to content

Commit 2b2bb5d

Browse files
authored
Merge pull request #2803 from calumgrant/cs/stackalloc-expr
C#: Handle implicitly-typed stackallocs
2 parents af4a6e4 + fb6da0b commit 2b2bb5d

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

change-notes/1.24/analysis-csharp.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ The following changes in version 1.24 affect C# analysis in all applications.
2727
## Changes to code extraction
2828

2929
* Tuple expressions, for example `(int,bool)` in `default((int,bool))` are now extracted correctly.
30-
* Expression nullability flow state is extracted.
30+
* Expression nullability flow state is extracted.
31+
* Implicitly typed `stackalloc` expressions are now extracted correctly.
3132

3233
## Changes to libraries
3334

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ArrayCreation.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ class StackAllocArrayCreation : ExplicitArrayCreation<StackAllocArrayCreationExp
9393
public static Expression Create(ExpressionNodeInfo info) => new StackAllocArrayCreation(info).TryPopulate();
9494
}
9595

96+
class ImplicitStackAllocArrayCreation : ArrayCreation<ImplicitStackAllocArrayCreationExpressionSyntax>
97+
{
98+
ImplicitStackAllocArrayCreation(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.ARRAY_CREATION)) { }
99+
100+
public static Expression Create(ExpressionNodeInfo info) => new ImplicitStackAllocArrayCreation(info).TryPopulate();
101+
102+
protected override void PopulateExpression(TextWriter trapFile)
103+
{
104+
ArrayInitializer.Create(new ExpressionNodeInfo(cx, Syntax.Initializer, this, -1));
105+
trapFile.implicitly_typed_array_creation(this);
106+
}
107+
}
108+
96109
class ImplicitArrayCreation : ArrayCreation<ImplicitArrayCreationExpressionSyntax>
97110
{
98111
ImplicitArrayCreation(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.ARRAY_CREATION)) { }

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Factory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ internal static Expression Create(ExpressionNodeInfo info)
207207
case SyntaxKind.StackAllocArrayCreationExpression:
208208
return StackAllocArrayCreation.Create(info);
209209

210+
case SyntaxKind.ImplicitStackAllocArrayCreationExpression:
211+
return ImplicitStackAllocArrayCreation.Create(info);
212+
210213
case SyntaxKind.ArgListExpression:
211214
return ArgList.Create(info);
212215

csharp/ql/test/library-tests/csharp7.3/ArrayCreations.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
| csharp73.cs:11:20:11:37 | array creation of type Char[] | 0 | csharp73.cs:11:20:11:37 | 1 |
44
| csharp73.cs:12:20:12:38 | array creation of type Char* | 0 | csharp73.cs:12:36:12:37 | 10 |
55
| csharp73.cs:13:20:13:31 | array creation of type Char[] | 0 | csharp73.cs:13:29:13:30 | 10 |
6-
| csharp73.cs:21:23:21:33 | array creation of type Int32[] | 0 | csharp73.cs:21:31:21:32 | 10 |
6+
| csharp73.cs:22:23:22:33 | array creation of type Int32[] | 0 | csharp73.cs:22:31:22:32 | 10 |

csharp/ql/test/library-tests/csharp7.3/ArrayElements.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
| csharp73.cs:9:20:9:49 | array creation of type Char* | 1 | csharp73.cs:9:45:9:47 | y |
33
| csharp73.cs:10:20:10:45 | array creation of type Char* | 0 | csharp73.cs:10:41:10:43 | x |
44
| csharp73.cs:11:20:11:37 | array creation of type Char[] | 0 | csharp73.cs:11:33:11:35 | x |
5+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 0 | csharp73.cs:14:35:14:35 | 1 |
6+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 1 | csharp73.cs:14:38:14:38 | 2 |
7+
| csharp73.cs:14:20:14:43 | array creation of type Int32* | 2 | csharp73.cs:14:41:14:41 | 3 |

csharp/ql/test/library-tests/csharp7.3/csharp73.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ unsafe void Fn()
1111
var arr3 = new char[] { 'x' };
1212
var arr4 = stackalloc char[10];
1313
var arr5 = new char[10];
14+
var arr6 = stackalloc[] { 1, 2, 3 };
1415
}
1516
}
1617

0 commit comments

Comments
 (0)