Skip to content

Commit 58babdd

Browse files
authored
Merge pull request #1187 from calumgrant/cs/expression-null
C#: Fix FP in cs/constant-condition
2 parents c112a4d + b628060 commit 58babdd

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

change-notes/1.21/analysis-csharp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| **Query** | **Expected impact** | **Change** |
66
|------------------------------|------------------------|-----------------------------------|
77
| Class defines a field that uses an ICryptoTransform class in a way that would be unsafe for concurrent threads (`cs/thread-unsafe-icryptotransform-field-in-class`) | Fewer false positive results | The criteria for a result has changed to include nested properties, nested fields and collections. The format of the alert message has changed to highlight the static field. |
8+
| Constant condition (`cs/constant-condition`) | Fewer false positive results | Results have been removed where the `null` value is in a conditional expression on the left hand side of a null-coalescing expression. For example, in `(a ? b : null) ?? c`, `null` is not considered to be a constant condition. |
89

910
## Changes to code extraction
1011

csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ class ConstantNullnessCondition extends ConstantCondition {
7171

7272
ConstantNullnessCondition() {
7373
forex(ControlFlow::Node cfn | cfn = this.getAControlFlowNode() |
74-
exists(ControlFlow::SuccessorTypes::NullnessSuccessor t | exists(cfn.getASuccessorByType(t)) |
75-
b = t.getValue()
74+
exists(ControlFlow::SuccessorTypes::NullnessSuccessor t, ControlFlow::Node s |
75+
s = cfn.getASuccessorByType(t) |
76+
b = t.getValue() and
77+
not s.isJoin()
7678
) and
7779
strictcount(ControlFlow::SuccessorType t | exists(cfn.getASuccessorByType(t))) = 1
7880
)

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void M2(int i)
5050
j = (int?)i ?? 1; // BAD
5151
s = ""?.CommaJoinWith(s); // BAD
5252
s = s ?? ""; // GOOD
53+
s = (i==0 ? s : null) ?? s; // GOOD
54+
var k = (i==0 ? s : null)?.Length; // GOOD
5355
}
5456
}
5557

csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
| ConstantCondition.cs:49:17:49:18 | "" | Expression is never 'null'. |
55
| ConstantCondition.cs:50:13:50:19 | (...) ... | Expression is never 'null'. |
66
| ConstantCondition.cs:51:13:51:14 | "" | Expression is never 'null'. |
7-
| ConstantCondition.cs:62:18:62:18 | 2 | Pattern never matches. |
8-
| ConstantCondition.cs:64:18:64:18 | 3 | Pattern always matches. |
9-
| ConstantCondition.cs:75:18:75:20 | access to type Int32 | Pattern never matches. |
7+
| ConstantCondition.cs:64:18:64:18 | 2 | Pattern never matches. |
8+
| ConstantCondition.cs:66:18:66:18 | 3 | Pattern always matches. |
9+
| ConstantCondition.cs:77:18:77:20 | access to type Int32 | Pattern never matches. |
1010
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
1111
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
1212
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |

0 commit comments

Comments
 (0)