Skip to content

Commit 6e4ef50

Browse files
committed
C#: Remove uses of expanded assignment.
1 parent f86fa0b commit 6e4ef50

File tree

9 files changed

+18
-132
lines changed

9 files changed

+18
-132
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ module AssignableInternal {
379379
or
380380
exists(Assignment ass | ac = ass.getLValue() |
381381
result = ass.getRValue() and
382-
not ass.(AssignOperation).hasExpandedAssignment()
382+
// TODO: Maybe be an issue for event accessor calls.
383+
not ass instanceof AssignOperation
383384
)
384385
}
385386
}

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 & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,13 @@ class CfgScope extends Element, @top_level_exprorstmt_parent {
8989

9090
private class TAstNode = @callable or @control_flow_element;
9191

92-
private Element getAChild(Element p) {
93-
result = p.getAChild() or
94-
result = p.(AssignOperation).getExpandedAssignment()
95-
}
96-
9792
pragma[nomagic]
9893
private predicate astNode(Element e) {
9994
e = any(@top_level_exprorstmt_parent p | not p instanceof Attribute)
10095
or
10196
exists(Element parent |
10297
astNode(parent) and
103-
e = getAChild(parent)
98+
e = parent.getAChild()
10499
)
105100
}
106101

@@ -521,7 +516,6 @@ module Expressions {
521516
not this instanceof LogicalOrExpr and
522517
not this instanceof NullCoalescingExpr and
523518
not this instanceof ConditionalExpr and
524-
not this instanceof AssignOperationWithExpandedAssignment and
525519
not this instanceof ConditionallyQualifiedExpr and
526520
not this instanceof ThrowExpr and
527521
not this instanceof ObjectCreation and
@@ -619,7 +613,7 @@ module Expressions {
619613
def.getExpr() = this and
620614
def.getTargetAccess().(WriteAccess) instanceof QualifiableExpr and
621615
not def instanceof AssignableDefinitions::OutRefDefinition and
622-
not this instanceof AssignOperationWithExpandedAssignment
616+
not def instanceof AssignableDefinitions::AssignOperationDefinition
623617
}
624618

625619
/**
@@ -802,26 +796,6 @@ module Expressions {
802796
}
803797
}
804798

805-
/**
806-
* An assignment operation that has an expanded version. We use the expanded
807-
* version in the control flow graph in order to get better data flow / taint
808-
* tracking.
809-
*/
810-
private class AssignOperationWithExpandedAssignment extends ControlFlowTree instanceof AssignOperation
811-
{
812-
private Expr expanded;
813-
814-
AssignOperationWithExpandedAssignment() { expanded = this.getExpandedAssignment() }
815-
816-
final override predicate first(AstNode first) { first(expanded, first) }
817-
818-
final override predicate last(AstNode last, Completion c) { last(expanded, last, c) }
819-
820-
final override predicate propagatesAbnormal(AstNode child) { none() }
821-
822-
final override predicate succ(AstNode pred, AstNode succ, Completion c) { none() }
823-
}
824-
825799
/** A conditionally qualified expression. */
826800
private class ConditionallyQualifiedExpr extends PostOrderTree instanceof QualifiableExpr {
827801
private Expr qualifier;
@@ -1579,7 +1553,7 @@ module Statements {
15791553
/** Gets a child of `cfe` that is in CFG scope `scope`. */
15801554
pragma[noinline]
15811555
private ControlFlowElement getAChildInScope(AstNode cfe, Callable scope) {
1582-
result = getAChild(cfe) and
1556+
result = cfe.getAChild() and
15831557
scope = result.getEnclosingCallable()
15841558
}
15851559

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ 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
205+
exists(DynamicOperatorCall doc, AssignOperation ae |
207206
dma = ae.getLValue() and
208207
doc = ae.getRValue()
209208
|

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: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ import csharp
1818
/** A use of `&` or `|` on operands of type boolean. */
1919
class NonShortCircuit extends BinaryBitwiseOperation {
2020
NonShortCircuit() {
21-
(
22-
this instanceof BitwiseAndExpr
23-
or
24-
this instanceof BitwiseOrExpr
25-
) and
26-
not exists(AssignBitwiseOperation abo | abo.getExpandedAssignment().getRValue() = this) and
27-
this.getLeftOperand().getType() instanceof BoolType and
28-
this.getRightOperand().getType() instanceof BoolType
21+
this instanceof BitwiseAndExpr
22+
or
23+
this instanceof BitwiseOrExpr
2924
}
3025

3126
pragma[nomagic]

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)