Skip to content

Commit 998d720

Browse files
committed
C#: Remove uses of expanded assignment.
1 parent 0b79ea2 commit 998d720

9 files changed

Lines changed: 18 additions & 129 deletions

File tree

csharp/ql/lib/semmle/code/csharp/Assignable.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,10 @@ module AssignableInternal {
379379
or
380380
exists(Assignment ass | ac = ass.getLValue() |
381381
result = ass.getRValue() and
382-
not ass.(AssignOperation).hasExpandedAssignment()
382+
not ass instanceof AssignOperation
383383
)
384+
or
385+
exists(AssignOperation ao | ac = ao.getLeftOperand() | result = ao)
384386
}
385387
}
386388

csharp/ql/lib/semmle/code/csharp/ExprOrStmtParent.qll

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ExprOrStmtParent extends Element, @exprorstmt_parent {
2020

2121
/** Gets the `i`th child expression of this element (zero-based). */
2222
final Expr getChildExpr(int i) {
23-
expr_parent_adjusted(result, i, this) or
23+
expr_parent(result, i, this) or
2424
expr_parent_top_level_adjusted(result, i, this)
2525
}
2626

@@ -119,66 +119,14 @@ private module Cached {
119119
}
120120

121121
/**
122-
* The `expr_parent()` relation adjusted for expandable assignments. For example,
123-
* the assignment `x += y` is extracted as
124-
*
125-
* ```
126-
* +=
127-
* |
128-
* 2
129-
* |
130-
* =
131-
* / \
132-
* 1 0
133-
* / \
134-
* x +
135-
* / \
136-
* 1 0
137-
* / \
138-
* x y
139-
* ```
140-
*
141-
* in order to be able to retrieve the expanded assignment `x = x + y` as the 2nd
142-
* child. This predicate changes the diagram above into
143-
*
144-
* ```
145-
* +=
146-
* / \
147-
* 1 0
148-
* / \
149-
* x y
150-
* ```
122+
* Use `expr_parent` instead.
151123
*/
152124
cached
153-
predicate expr_parent_adjusted(Expr child, int i, ControlFlowElement parent) {
154-
if parent instanceof AssignOperation
155-
then
156-
parent =
157-
any(AssignOperation ao |
158-
exists(AssignExpr ae | ae = ao.getExpandedAssignment() |
159-
i = 0 and
160-
exists(Expr right |
161-
// right = `x + y`
162-
expr_parent(right, 0, ae)
163-
|
164-
expr_parent(child, 1, right)
165-
)
166-
or
167-
i = 1 and
168-
expr_parent(child, 1, ae)
169-
)
170-
or
171-
not ao.hasExpandedAssignment() and
172-
expr_parent(child, i, parent)
173-
)
174-
else expr_parent(child, i, parent)
175-
}
125+
deprecated predicate expr_parent_adjusted(Expr child, int i, ControlFlowElement parent) { none() }
176126

177127
private Expr getAChildExpr(ExprOrStmtParent parent) {
178128
result = parent.getAChildExpr() and
179129
not result = parent.(DeclarationWithGetSetAccessors).getExpressionBody()
180-
or
181-
result = parent.(AssignOperation).getExpandedAssignment()
182130
}
183131

184132
private ControlFlowElement getAChild(ExprOrStmtParent parent) {

csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,13 @@ class CfgScope extends Element, @top_level_exprorstmt_parent {
6262

6363
private class TAstNode = @callable or @control_flow_element;
6464

65-
private Element getAChild(Element p) {
66-
result = p.getAChild() or
67-
result = p.(AssignOperation).getExpandedAssignment()
68-
}
69-
7065
pragma[nomagic]
7166
private predicate astNode(Element e) {
7267
e = any(@top_level_exprorstmt_parent p | not p instanceof Attribute)
7368
or
7469
exists(Element parent |
7570
astNode(parent) and
76-
e = getAChild(parent)
71+
e = parent.getAChild()
7772
)
7873
}
7974

@@ -494,7 +489,6 @@ module Expressions {
494489
not this instanceof LogicalOrExpr and
495490
not this instanceof NullCoalescingExpr and
496491
not this instanceof ConditionalExpr and
497-
not this instanceof AssignOperationWithExpandedAssignment and
498492
not this instanceof ConditionallyQualifiedExpr and
499493
not this instanceof ThrowExpr and
500494
not this instanceof ObjectCreation and
@@ -591,8 +585,7 @@ module Expressions {
591585
QualifiedAccessorWrite() {
592586
def.getExpr() = this and
593587
def.getTargetAccess().(WriteAccess) instanceof QualifiableExpr and
594-
not def instanceof AssignableDefinitions::OutRefDefinition and
595-
not this instanceof AssignOperationWithExpandedAssignment
588+
not def instanceof AssignableDefinitions::OutRefDefinition
596589
}
597590

598591
/**
@@ -775,26 +768,6 @@ module Expressions {
775768
}
776769
}
777770

778-
/**
779-
* An assignment operation that has an expanded version. We use the expanded
780-
* version in the control flow graph in order to get better data flow / taint
781-
* tracking.
782-
*/
783-
private class AssignOperationWithExpandedAssignment extends ControlFlowTree instanceof AssignOperation
784-
{
785-
private Expr expanded;
786-
787-
AssignOperationWithExpandedAssignment() { expanded = this.getExpandedAssignment() }
788-
789-
final override predicate first(AstNode first) { first(expanded, first) }
790-
791-
final override predicate last(AstNode last, Completion c) { last(expanded, last, c) }
792-
793-
final override predicate propagatesAbnormal(AstNode child) { none() }
794-
795-
final override predicate succ(AstNode pred, AstNode succ, Completion c) { none() }
796-
}
797-
798771
/** A conditionally qualified expression. */
799772
private class ConditionallyQualifiedExpr extends PostOrderTree instanceof QualifiableExpr {
800773
private Expr qualifier;
@@ -1552,7 +1525,7 @@ module Statements {
15521525
/** Gets a child of `cfe` that is in CFG scope `scope`. */
15531526
pragma[noinline]
15541527
private ControlFlowElement getAChildInScope(AstNode cfe, Callable scope) {
1555-
result = getAChild(cfe) and
1528+
result = cfe.getAChild() and
15561529
scope = result.getEnclosingCallable()
15571530
}
15581531

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,9 @@ private module Internal {
202202
private predicate isPotentialEventCall(
203203
AssignArithmeticOperation aao, DynamicMemberAccess dma, string name
204204
) {
205-
exists(DynamicOperatorCall doc, AssignExpr ae |
206-
ae = aao.getExpandedAssignment() and
207-
dma = ae.getLValue() and
208-
doc = ae.getRValue()
209-
|
205+
aao instanceof DynamicOperatorCall and
206+
dma = aao.getLeftOperand() and
207+
(
210208
aao instanceof AssignAddExpr and
211209
name = "add_" + dma.getLateBoundTargetName()
212210
or

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,14 @@ class AssignOperation extends Assignment, OperatorCall, @assign_op_expr {
7171
override string getOperator() { none() }
7272

7373
/**
74-
* Gets the expanded version of this assignment operation, if any.
75-
*
76-
* For example, if this assignment operation is `x += y` then
77-
* the expanded assignment is `x = x + y`.
78-
*
79-
* If an expanded version exists, then it is used in the control
80-
* flow graph.
74+
* Expanded versions of compound assignments are no longer extracted.
8175
*/
82-
AssignExpr getExpandedAssignment() { none() }
76+
deprecated AssignExpr getExpandedAssignment() { none() }
8377

8478
/**
85-
* Holds if this assignment operation has an expanded version.
86-
*
87-
* For example, if this assignment operation is `x += y` then
88-
* it has the expanded version `x = x + y`.
89-
*
90-
* If an expanded version exists, then it is used in the control
91-
* flow graph.
79+
* Expanded versions of compound assignments are no longer extracted.
9280
*/
93-
predicate hasExpandedAssignment() { exists(this.getExpandedAssignment()) }
81+
deprecated predicate hasExpandedAssignment() { none() }
9482

9583
override Expr getLeftOperand() { result = this.getChild(0) }
9684

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,11 @@ class Expr extends ControlFlowElement, @expr {
6565
/** Gets the enclosing callable of this expression, if any. */
6666
override Callable getEnclosingCallable() { enclosingCallable(this, result) }
6767

68-
pragma[nomagic]
69-
private predicate isExpandedAssignmentRValueDescendant() {
70-
this =
71-
any(AssignOperation op).getExpandedAssignment().getRValue().getChildExpr(0).getAChildExpr()
72-
or
73-
exists(Expr parent |
74-
parent.isExpandedAssignmentRValueDescendant() and
75-
this = parent.getAChildExpr()
76-
)
77-
}
78-
7968
/**
8069
* Holds if this expression is generated by the compiler and does not appear
8170
* explicitly in the source code.
8271
*/
83-
final predicate isImplicit() {
84-
compiler_generated(this) or
85-
this.isExpandedAssignmentRValueDescendant()
86-
}
72+
final predicate isImplicit() { compiler_generated(this) }
8773

8874
/**
8975
* Gets an expression that is the result of stripping (recursively) all

csharp/ql/src/Likely Bugs/DangerousNonShortCircuitLogic.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class NonShortCircuit extends BinaryBitwiseOperation {
2323
or
2424
this instanceof BitwiseOrExpr
2525
) and
26-
not exists(AssignBitwiseOperation abo | abo.getExpandedAssignment().getRValue() = this) and
2726
this.getLeftOperand().getType() instanceof BoolType and
2827
this.getRightOperand().getType() instanceof BoolType
2928
}

csharp/ql/src/Performance/StringConcatenationInLoop.ql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class StringCat extends AddExpr {
2323
* where `v` is a simple variable (and not, for example, a property).
2424
*/
2525
predicate isSelfConcatAssignExpr(AssignExpr e, Variable v) {
26-
not e = any(AssignAddExpr a).getExpandedAssignment() and
2726
exists(VariableAccess use |
2827
stringCatContains(e.getRValue(), use) and
2928
use.getTarget() = e.getTargetVariable() and

csharp/ql/src/Security Features/InsecureRandomness.ql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ module Random {
8989
e = any(SensitiveLibraryParameter v).getAnAssignedArgument()
9090
or
9191
// Assignment operation, e.g. += or similar
92-
exists(AssignOperation ao |
93-
ao.getRValue() = e and
94-
// "expanded" assignments will be covered by simple assignment
95-
not ao.hasExpandedAssignment()
96-
|
92+
exists(AssignOperation ao | ao.getRValue() = e |
9793
ao.getLValue() = any(SensitiveVariable v).getAnAccess() or
9894
ao.getLValue() = any(SensitiveProperty v).getAnAccess() or
9995
ao.getLValue() = any(SensitiveLibraryParameter v).getAnAccess()

0 commit comments

Comments
 (0)