Skip to content

Commit febd7bf

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

File tree

8 files changed

+23
-11
lines changed

8 files changed

+23
-11
lines changed

csharp/ql/examples/snippets/ternary_conditional.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import csharp
1111

1212
from ConditionalExpr e
1313
where
14-
e.getThen().stripImplicitCasts() != e.getElse().stripImplicitCasts() and
14+
e.getThen().stripImplicit() != e.getElse().stripImplicit() and
1515
not e.getThen().getType() instanceof NullType and
1616
not e.getElse().getType() instanceof NullType
1717
select e

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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,18 @@ class Expr extends ControlFlowElement, @expr {
8282
Expr stripCasts() { result = this }
8383

8484
/**
85+
* DEPRECATED: Use `stripImplicit` instead.
86+
*
8587
* Gets an expression that is the result of stripping (recursively) all
8688
* implicit casts from this expression, if any.
8789
*/
88-
Expr stripImplicitCasts() { result = this }
90+
deprecated Expr stripImplicitCasts() { result = this.stripImplicit() }
91+
92+
/**
93+
* Gets an expression that is the result of stripping (recursively) all
94+
* implicit casts and implicit ToString calls from this expression, if any.
95+
*/
96+
Expr stripImplicit() { result = this }
8997

9098
/**
9199
* Gets the explicit parameter name used to pass this expression as an
@@ -714,8 +722,8 @@ class Cast extends Expr {
714722

715723
override Expr stripCasts() { result = this.getExpr().stripCasts() }
716724

717-
override Expr stripImplicitCasts() {
718-
if this.isImplicit() then result = this.getExpr().stripImplicitCasts() else result = this
725+
override Expr stripImplicit() {
726+
if this.isImplicit() then result = this.getExpr().stripImplicit() else result = this
719727
}
720728
}
721729

csharp/ql/src/Likely Bugs/Dynamic/BadDynamicCall.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ abstract class BadDynamicCall extends DynamicExpr {
4444
ultimateSsaDef = ssaDef.getAnUltimateDefinition()
4545
|
4646
ultimateSsaDef.getADefinition() =
47-
any(AssignableDefinition def | source = def.getSource().stripImplicitCasts())
47+
any(AssignableDefinition def | source = def.getSource().stripImplicit())
4848
or
4949
ultimateSsaDef.getADefinition() =
5050
any(AssignableDefinitions::ImplicitParameterDefinition p |
51-
source = p.getParameter().getAnAssignedValue().stripImplicitCasts()
51+
source = p.getParameter().getAnAssignedValue().stripImplicit()
5252
)
5353
)
5454
}

csharp/ql/src/Likely Bugs/ObjectComparison.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ReferenceEqualityTestOnObject extends EqualityOperation {
2828
exists(getObjectOperand(this)) and
2929
// Neither operand is 'null'.
3030
not this.getAnOperand() instanceof NullLiteral and
31-
not exists(Type t | t = this.getAnOperand().stripImplicitCasts().getType() |
31+
not exists(Type t | t = this.getAnOperand().stripImplicit().getType() |
3232
t instanceof NullType or
3333
t instanceof ValueType
3434
) and

0 commit comments

Comments
 (0)