Skip to content

Commit d5073df

Browse files
authored
Merge pull request github#16186 from michaelnebel/csharp/suppressnullablefix
C#: Fix issue with suppress nullable warning directly on a method call.
2 parents 611cf23 + cbb5d43 commit d5073df

File tree

11 files changed

+74
-2
lines changed

11 files changed

+74
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal static class CallTypeExtensions
99
/// </summary>
1010
public static ExprKind AdjustKind(this Expression.CallType ct, ExprKind k)
1111
{
12-
if (k == ExprKind.ADDRESS_OF)
12+
if (k == ExprKind.ADDRESS_OF || k == ExprKind.SUPPRESS_NULLABLE_WARNING)
1313
{
1414
return k;
1515
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ private PostfixUnary(ExpressionNodeInfo info, ExprKind kind, ExpressionSyntax op
2121
protected override void PopulateExpression(TextWriter trapFile)
2222
{
2323
Create(Context, operand, this, 0);
24-
OperatorCall(trapFile, Syntax);
2524

2625
if ((operatorKind == ExprKind.POST_INCR || operatorKind == ExprKind.POST_DECR) &&
2726
Kind == ExprKind.OPERATOR_INVOCATION)
2827
{
28+
OperatorCall(trapFile, Syntax);
2929
trapFile.mutator_invocation_mode(this, 2);
3030
}
3131
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Extracting suppress nullable warning expressions did not work when applied directly to a method call (like `System.Console.Readline()!`). This has been fixed.

csharp/ql/test/library-tests/expressions/ConstructorInitializers.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| file://:0:0:0:0 | Rectangle | expressions.cs:351:18:351:26 | call to constructor Object | file://:0:0:0:0 | Object |
2323
| file://:0:0:0:0 | Rectangle2 | expressions.cs:361:18:361:27 | call to constructor Object | file://:0:0:0:0 | Object |
2424
| file://:0:0:0:0 | ReducedClass | ReducedExpression.cs:2:7:2:18 | call to constructor Object | file://:0:0:0:0 | Object |
25+
| file://:0:0:0:0 | SuppressNullableWarning | expressions.cs:522:11:522:33 | call to constructor Object | file://:0:0:0:0 | Object |
2526
| file://:0:0:0:0 | TestConversionOperator | expressions.cs:330:11:330:32 | call to constructor Object | file://:0:0:0:0 | Object |
2627
| file://:0:0:0:0 | TestCreations | expressions.cs:383:18:383:30 | call to constructor Object | file://:0:0:0:0 | Object |
2728
| file://:0:0:0:0 | TestUnaryOperator | expressions.cs:292:11:292:27 | call to constructor Object | file://:0:0:0:0 | Object |

csharp/ql/test/library-tests/expressions/PrintAst.expected

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,3 +2406,26 @@ expressions.cs:
24062406
# 520| -1: [TypeMention] object
24072407
# 520| 3: [ConstructorInitializer] call to constructor ClassC1
24082408
# 520| 0: [ParameterAccess] access to parameter oc2
2409+
# 522| 24: [Class] SuppressNullableWarning
2410+
# 525| 5: [Method] Api
2411+
# 525| -1: [TypeMention] object
2412+
# 525| 4: [ObjectCreation] object creation of type Object
2413+
# 525| 0: [TypeMention] object
2414+
# 527| 6: [Method] Test
2415+
# 527| -1: [TypeMention] Void
2416+
#-----| 2: (Parameters)
2417+
# 527| 0: [Parameter] arg0
2418+
# 527| -1: [TypeMention] object
2419+
# 528| 4: [BlockStmt] {...}
2420+
# 529| 0: [LocalVariableDeclStmt] ... ...;
2421+
# 529| 0: [LocalVariableDeclAndInitExpr] Object x = ...
2422+
# 529| -1: [TypeMention] object
2423+
# 529| 0: [LocalVariableAccess] access to local variable x
2424+
# 529| 1: [SuppressNullableWarningExpr] ...!
2425+
# 529| 0: [ParameterAccess] access to parameter arg0
2426+
# 530| 1: [LocalVariableDeclStmt] ... ...;
2427+
# 530| 0: [LocalVariableDeclAndInitExpr] Object y = ...
2428+
# 530| -1: [TypeMention] object
2429+
# 530| 0: [LocalVariableAccess] access to local variable y
2430+
# 530| 1: [SuppressNullableWarningExpr] ...!
2431+
# 530| 0: [MethodCall] call to method Api

csharp/ql/test/library-tests/expressions/QualifiableExpr.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@
7070
| expressions.cs:483:17:483:26 | access to field value | expressions.cs:483:17:483:20 | this access |
7171
| expressions.cs:488:32:488:39 | access to field value | expressions.cs:488:32:488:33 | access to parameter c1 |
7272
| expressions.cs:488:43:488:50 | access to field value | expressions.cs:488:43:488:44 | access to parameter c2 |
73+
| expressions.cs:530:21:530:25 | call to method Api | expressions.cs:530:21:530:25 | this access |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| expressions.cs:529:21:529:25 | ...! |
2+
| expressions.cs:530:21:530:26 | ...! |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import csharp
2+
3+
select any(SuppressNullableWarningExpr e)

csharp/ql/test/library-tests/expressions/expressions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,16 @@ struct MyInlineArray
518518
class ClassC1(object oc1) { }
519519

520520
class ClassC2(object oc2) : ClassC1(oc2) { }
521+
522+
class SuppressNullableWarning
523+
{
524+
525+
public object? Api() => new object();
526+
527+
public void Test(object? arg0)
528+
{
529+
var x = arg0!;
530+
var y = Api()!;
531+
}
532+
}
521533
}

csharp/ql/test/query-tests/Security Features/CWE-089/SqlInjection.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ public void GetDataSetByCategory()
9595
var result = new DataSet();
9696
adapter.Fill(result);
9797
}
98+
99+
// BAD: Input from the command line. (also implicitly check flow via suppress nullable warning `!`)
100+
using (var connection = new SqlConnection(connectionString))
101+
{
102+
var queryString = "SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='"
103+
+ Console.ReadLine()! + "' ORDER BY PRICE";
104+
var cmd = new SqlCommand(queryString);
105+
var adapter = new SqlDataAdapter(cmd);
106+
var result = new DataSet();
107+
adapter.Fill(result);
108+
}
98109
}
99110

100111
System.Windows.Forms.TextBox box1;

0 commit comments

Comments
 (0)