Skip to content

Commit cd2db7b

Browse files
committed
Java: Make Assignment extend BinaryExpr.
1 parent 3c129fc commit cd2db7b

File tree

15 files changed

+27
-80
lines changed

15 files changed

+27
-80
lines changed

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class ArrayInit extends Expr, @arrayinit {
392392
* element assignments since there the assignment destination is not directly
393393
* the array variable but instead an `ArrayAccess`.
394394
*/
395-
class Assignment extends Expr, @assignment {
395+
class Assignment extends BinaryExpr, @assignment {
396396
/** Gets the destination (left-hand side) of the assignment. */
397397
Expr getDest() { result.isNthChildOf(this, 0) }
398398

@@ -417,6 +417,8 @@ class Assignment extends Expr, @assignment {
417417
* For example, `x = 23`.
418418
*/
419419
class AssignExpr extends Assignment, @assignexpr {
420+
override string getOp() { result = "=" }
421+
420422
override string getAPrimaryQlClass() { result = "AssignExpr" }
421423
}
422424

@@ -445,7 +447,7 @@ class AssignOp extends Assignment, @assignop {
445447
override Expr getSource() { result.getParent() = this }
446448

447449
/** Gets a string representation of the assignment operator of this compound assignment. */
448-
/*abstract*/ string getOp() { result = "??=" }
450+
/*abstract*/ override string getOp() { result = "??=" }
449451

450452
/** Gets a printable representation of this expression. */
451453
override string toString() { result = "..." + this.getOp() + "..." }

java/ql/lib/semmle/code/java/PrettyPrintAst.qll

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,6 @@ private class PpArrayInit extends PpAst, ArrayInit {
207207
override PpAst getChild(int i) { exists(int j | result = this.getInit(j) and i = 1 + 2 * j) }
208208
}
209209

210-
private class PpAssignment extends PpAst, Assignment {
211-
override string getPart(int i) {
212-
i = 1 and
213-
this instanceof AssignExpr and
214-
result = " = "
215-
or
216-
i = 1 and
217-
result = " " + this.(AssignOp).getOp() + " "
218-
}
219-
220-
override PpAst getChild(int i) {
221-
i = 0 and result = this.getDest()
222-
or
223-
i = 2 and result = this.getRhs()
224-
}
225-
}
226-
227210
private class PpLiteral extends PpAst, Literal {
228211
override string getPart(int i) { i = 0 and result = this.getLiteral() }
229212
}

java/ql/lib/semmle/code/java/arithmetic/Overflow.qll

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ class ArithExpr extends Expr {
9393
) and
9494
forall(Expr e |
9595
e = this.(BinaryExpr).getAnOperand() or
96-
e = this.(UnaryAssignExpr).getOperand() or
97-
e = this.(AssignOp).getSource()
96+
e = this.(UnaryAssignExpr).getOperand()
9897
|
9998
e.getType() instanceof NumType
10099
)
@@ -114,21 +113,17 @@ class ArithExpr extends Expr {
114113
*/
115114
Expr getLeftOperand() {
116115
result = this.(BinaryExpr).getLeftOperand() or
117-
result = this.(UnaryAssignExpr).getOperand() or
118-
result = this.(AssignOp).getDest()
116+
result = this.(UnaryAssignExpr).getOperand()
119117
}
120118

121119
/**
122120
* Gets the right-hand operand if this is a binary expression.
123121
*/
124-
Expr getRightOperand() {
125-
result = this.(BinaryExpr).getRightOperand() or result = this.(AssignOp).getRhs()
126-
}
122+
Expr getRightOperand() { result = this.(BinaryExpr).getRightOperand() }
127123

128124
/** Gets an operand of this arithmetic expression. */
129125
Expr getAnOperand() {
130126
result = this.(BinaryExpr).getAnOperand() or
131-
result = this.(UnaryAssignExpr).getOperand() or
132-
result = this.(AssignOp).getSource()
127+
result = this.(UnaryAssignExpr).getOperand()
133128
}
134129
}

java/ql/lib/semmle/code/java/controlflow/Guards.qll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,15 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
179179
}
180180
}
181181

182-
abstract private class BinExpr extends Expr {
183-
Expr getAnOperand() {
184-
result = this.(BinaryExpr).getAnOperand() or result = this.(AssignOp).getSource()
185-
}
186-
}
187-
188-
class AndExpr extends BinExpr {
182+
class AndExpr extends BinaryExpr {
189183
AndExpr() {
190184
this instanceof AndBitwiseExpr or
191185
this instanceof AndLogicalExpr or
192186
this instanceof AssignAndExpr
193187
}
194188
}
195189

196-
class OrExpr extends BinExpr {
190+
class OrExpr extends BinaryExpr {
197191
OrExpr() {
198192
this instanceof OrBitwiseExpr or
199193
this instanceof OrLogicalExpr or

java/ql/lib/semmle/code/java/dataflow/Nullness.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@ private predicate unboxed(Expr e) {
5353
assign.getDest().getType() instanceof PrimitiveType and assign.getSource() = e
5454
)
5555
or
56-
exists(AssignOp assign | assign.getSource() = e and assign.getType() instanceof PrimitiveType)
57-
or
5856
exists(EqualityTest eq |
5957
eq.getAnOperand() = e and eq.getAnOperand().getType() instanceof PrimitiveType
6058
)
6159
or
6260
exists(BinaryExpr bin |
6361
bin.getAnOperand() = e and
6462
not bin instanceof EqualityTest and
63+
not bin instanceof AssignExpr and
6564
bin.getType() instanceof PrimitiveType
6665
)
6766
or

java/ql/lib/semmle/code/java/dataflow/RangeAnalysis.qll

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,7 @@ module Sem implements Semantic<Location> {
8686

8787
class ConstantIntegerExpr = RU::ConstantIntegerExpr;
8888

89-
abstract class BinaryExpr extends Expr {
90-
Expr getLeftOperand() {
91-
result = this.(J::BinaryExpr).getLeftOperand() or result = this.(J::AssignOp).getDest()
92-
}
93-
94-
Expr getRightOperand() {
95-
result = this.(J::BinaryExpr).getRightOperand() or result = this.(J::AssignOp).getRhs()
96-
}
97-
98-
final Expr getAnOperand() { result = this.getLeftOperand() or result = this.getRightOperand() }
99-
100-
final predicate hasOperands(Expr e1, Expr e2) {
101-
this.getLeftOperand() = e1 and this.getRightOperand() = e2
102-
or
103-
this.getLeftOperand() = e2 and this.getRightOperand() = e1
104-
}
105-
}
89+
class BinaryExpr = J::BinaryExpr;
10690

10791
class AddExpr extends BinaryExpr {
10892
AddExpr() { this instanceof J::AddExpr or this instanceof J::AssignAddExpr }

java/ql/lib/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,9 @@ module Private {
161161
this instanceof J::AssignUnsignedRightShiftExpr and result = TUnsignedRightShiftOp()
162162
}
163163

164-
Expr getLeftOperand() {
165-
result = this.(J::BinaryExpr).getLeftOperand() or result = this.(J::AssignOp).getDest()
166-
}
164+
Expr getLeftOperand() { result = this.(J::BinaryExpr).getLeftOperand() }
167165

168-
Expr getRightOperand() {
169-
result = this.(J::BinaryExpr).getRightOperand() or result = this.(J::AssignOp).getRhs()
170-
}
166+
Expr getRightOperand() { result = this.(J::BinaryExpr).getRightOperand() }
171167
}
172168

173169
predicate ssaRead = RU::ssaRead/2;

java/ql/lib/semmle/code/java/security/InsecureRandomnessQuery.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ module InsecureRandomnessConfig implements DataFlow::ConfigSig {
7373
predicate isBarrierOut(DataFlow::Node n) { isSink(n) }
7474

7575
predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
76-
n1.asExpr() = n2.asExpr().(BinaryExpr).getAnOperand()
76+
n1.asExpr() = n2.asExpr().(BinaryExpr).getAnOperand() and
77+
not n2.asExpr() instanceof AssignExpr
7778
or
7879
n1.asExpr() = n2.asExpr().(UnaryExpr).getOperand()
7980
or

java/ql/lib/semmle/code/java/security/NumericCastTaintedQuery.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ class RightShiftOp extends Expr {
3131
this instanceof AssignUnsignedRightShiftExpr
3232
}
3333

34-
private Expr getLhs() {
35-
this.(BinaryExpr).getLeftOperand() = result or
36-
this.(Assignment).getDest() = result
37-
}
34+
private Expr getLhs() { this.(BinaryExpr).getLeftOperand() = result }
3835

3936
/**
4037
* Gets the variable that is shifted.

java/ql/lib/semmle/code/java/security/RandomQuery.qll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ private module PredictableSeedFlowConfig implements DataFlow::ConfigSig {
5454
private module PredictableSeedFlow = DataFlow::Global<PredictableSeedFlowConfig>;
5555

5656
private predicate predictableCalcStep(Expr e1, Expr e2) {
57-
e2.(BinaryExpr).hasOperands(e1, any(PredictableSeedExpr p))
58-
or
59-
exists(AssignOp a | a = e2 | e1 = a.getDest() and a.getRhs() instanceof PredictableSeedExpr)
57+
e2.(BinaryExpr).hasOperands(e1, any(PredictableSeedExpr p)) and
58+
not e2 instanceof AssignExpr
6059
or
6160
exists(ConstructorCall cc, TypeNumber t | cc = e2 |
6261
cc.getArgument(0) = e1 and

0 commit comments

Comments
 (0)