Skip to content

Commit ac9c76b

Browse files
committed
C#: Modify compiler generated strip logic to also take generated ToString calls into account.
1 parent d3fad2b commit ac9c76b

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

csharp/ql/lib/semmle/code/csharp/commons/Constants.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ predicate isConstantComparison(ComparisonOperation co, boolean b) {
4343
private module ConstantComparisonOperation {
4444
private import semmle.code.csharp.commons.ComparisonTest
4545

46-
private SimpleType convertedType(Expr expr) { result = expr.stripImplicitCasts().getType() }
46+
private SimpleType convertedType(Expr expr) { result = expr.stripImplicit().getType() }
4747

4848
private int maxValue(Expr expr) {
4949
if convertedType(expr) instanceof IntegralType and exists(expr.getValue())

csharp/ql/lib/semmle/code/csharp/commons/Strings.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class ImplicitToStringExpr extends Expr {
4444
)
4545
or
4646
exists(AddExpr add, Expr o | o = add.getAnOperand() |
47-
o.stripImplicitCasts().getType() instanceof StringType and
48-
this = add.getOtherOperand(o)
47+
o.stripImplicit().getType() instanceof StringType and
48+
this = add.getOtherOperand(o).stripImplicit()
4949
)
5050
or
5151
this = any(InterpolatedStringExpr ise).getAnInsert()

csharp/ql/lib/semmle/code/csharp/dispatch/Dispatch.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ private module Internal {
857857
private predicate hasDynamicArg(int i, Type argumentType) {
858858
exists(Expr argument |
859859
argument = this.getArgument(i) and
860-
argument.stripImplicitCasts().getType() instanceof DynamicType and
860+
argument.stripImplicit().getType() instanceof DynamicType and
861861
argumentType = getAPossibleType(argument, _)
862862
)
863863
}

csharp/ql/lib/semmle/code/csharp/exprs/Call.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ class MethodCall extends Call, QualifiableExpr, LateBindableExpr, @method_invoca
281281
result = this.getArgument(i - 1)
282282
else result = this.getArgument(i)
283283
}
284+
285+
override Expr stripImplicit() {
286+
if this.isImplicit() then result = this.getQualifier().stripImplicit() else result = this
287+
}
284288
}
285289

286290
/**

csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ class Expr extends ControlFlowElement, @expr {
8585
* Gets an expression that is the result of stripping (recursively) all
8686
* implicit casts from this expression, if any.
8787
*/
88-
Expr stripImplicitCasts() { result = this }
88+
deprecated Expr stripImplicitCasts() { result = this }
89+
90+
/**
91+
* Gets an expression that is the result of stripping (recursively) all
92+
* implicit casts and implicit ToString calls from this expression, if any.
93+
*/
94+
Expr stripImplicit() { result = this }
8995

9096
/**
9197
* Gets the explicit parameter name used to pass this expression as an
@@ -714,8 +720,8 @@ class Cast extends Expr {
714720

715721
override Expr stripCasts() { result = this.getExpr().stripCasts() }
716722

717-
override Expr stripImplicitCasts() {
718-
if this.isImplicit() then result = this.getExpr().stripImplicitCasts() else result = this
723+
override Expr stripImplicit() {
724+
if this.isImplicit() then result = this.getExpr().stripImplicit() else result = this
719725
}
720726
}
721727

0 commit comments

Comments
 (0)