Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cf3196

Browse files
committed
Fix additional PR review findings
1 parent 01de550 commit 1cf3196

5 files changed

Lines changed: 101 additions & 111 deletions

File tree

csharp/ql/src/semmle/code/csharp/dataflow/internal/rangeanalysis/SignAnalysisCommon.qll

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,21 +328,20 @@ Sign exprSign(Expr e) {
328328

329329
/** Gets a possible sign for `e` from the signs of its child nodes. */
330330
private Sign specificSubExprSign(Expr e) {
331-
result = exprSign(getASubExpr(e))
331+
result = exprSign(getASubExprWithSameSign(e))
332332
or
333-
e =
334-
any(DivExpr div |
335-
result = exprSign(div.getLeftOperand()) and
336-
result != TZero() and
337-
div.getRightOperand().(RealLiteral).getValue().toFloat() = 0
338-
)
333+
exists(DivExpr div | div = e |
334+
result = exprSign(div.getLeftOperand()) and
335+
result != TZero() and
336+
div.getRightOperand().(RealLiteral).getValue().toFloat() = 0
337+
)
339338
or
340-
exists(UnaryExpr unary | unary = e |
339+
exists(UnaryOperation unary | unary = e |
341340
result = exprSign(unary.getOperand()).applyUnaryOp(unary.getOp())
342341
)
343342
or
344343
exists(Sign s1, Sign s2 | binaryOpSigns(e, s1, s2) |
345-
result = s1.applyBinaryOp(s2, e.(BinaryExpr).getOp())
344+
result = s1.applyBinaryOp(s2, e.(BinaryOperation).getOp())
346345
)
347346
}
348347

csharp/ql/src/semmle/code/csharp/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Private {
66
private import csharp as CS
77
private import ConstantUtils as CU
88
private import semmle.code.csharp.controlflow.Guards as G
9+
private import Sign
910
import Impl
1011

1112
class Guard = G::Guard;
@@ -42,7 +43,73 @@ module Private {
4243

4344
class DivExpr = CS::DivExpr;
4445

45-
class BinaryOperation = CS::BinaryOperation;
46+
/** Class to represent unary operation. */
47+
class UnaryOperation extends Expr {
48+
UnaryOperation() {
49+
this instanceof CS::PreIncrExpr or
50+
this instanceof CS::PreDecrExpr or
51+
this instanceof CS::UnaryMinusExpr or
52+
this instanceof CS::ComplementExpr
53+
}
54+
55+
/** Returns the operand of this expression. */
56+
Expr getOperand() {
57+
result = this.(CS::PreIncrExpr).getOperand() or
58+
result = this.(CS::PreDecrExpr).getOperand() or
59+
result = this.(CS::UnaryMinusExpr).getOperand() or
60+
result = this.(CS::ComplementExpr).getOperand()
61+
}
62+
63+
/** Returns the operation representing this expression. */
64+
TUnarySignOperation getOp() {
65+
this instanceof CS::PreIncrExpr and result = TIncOp()
66+
or
67+
this instanceof CS::PreDecrExpr and result = TDecOp()
68+
or
69+
this instanceof CS::UnaryMinusExpr and result = TNegOp()
70+
or
71+
this instanceof CS::ComplementExpr and result = TBitNotOp()
72+
}
73+
}
74+
75+
/** Class to represent binary operation. */
76+
class BinaryOperation extends CS::BinaryOperation {
77+
BinaryOperation() {
78+
this instanceof CS::AddExpr or
79+
this instanceof CS::SubExpr or
80+
this instanceof CS::MulExpr or
81+
this instanceof CS::DivExpr or
82+
this instanceof CS::RemExpr or
83+
this instanceof CS::BitwiseAndExpr or
84+
this instanceof CS::BitwiseOrExpr or
85+
this instanceof CS::BitwiseXorExpr or
86+
this instanceof CS::LShiftExpr or
87+
this instanceof CS::RShiftExpr
88+
}
89+
90+
/** Returns the operation representing this expression. */
91+
TBinarySignOperation getOp() {
92+
this instanceof CS::AddExpr and result = TAddOp()
93+
or
94+
this instanceof CS::SubExpr and result = TSubOp()
95+
or
96+
this instanceof CS::MulExpr and result = TMulOp()
97+
or
98+
this instanceof CS::DivExpr and result = TDivOp()
99+
or
100+
this instanceof CS::RemExpr and result = TRemOp()
101+
or
102+
this instanceof CS::BitwiseAndExpr and result = TBitAndOp()
103+
or
104+
this instanceof CS::BitwiseOrExpr and result = TBitOrOp()
105+
or
106+
this instanceof CS::BitwiseXorExpr and result = TBitXorOp()
107+
or
108+
this instanceof CS::LShiftExpr and result = TLShiftOp()
109+
or
110+
this instanceof CS::RShiftExpr and result = TRShiftOp()
111+
}
112+
}
46113

47114
predicate ssaRead = SU::ssaRead/2;
48115
}
@@ -203,7 +270,7 @@ private module Impl {
203270
}
204271

205272
/** Returns a sub expression of `e` for expression types where the sign depends on the child. */
206-
Expr getASubExpr(Expr e) {
273+
Expr getASubExprWithSameSign(Expr e) {
207274
result = e.(AssignExpr).getRValue() or
208275
result = e.(AssignOperation).getExpandedAssignment() or
209276
result = e.(UnaryPlusExpr).getOperand() or
@@ -218,74 +285,6 @@ private module Impl {
218285
result = e.(CastExpr).getExpr()
219286
}
220287

221-
/** Class to represent unary expressions. */
222-
class UnaryExpr extends Expr {
223-
UnaryExpr() {
224-
this instanceof PreIncrExpr or
225-
this instanceof PreDecrExpr or
226-
this instanceof UnaryMinusExpr or
227-
this instanceof ComplementExpr
228-
}
229-
230-
/** Returns the operand of this expression. */
231-
Expr getOperand() {
232-
result = this.(PreIncrExpr).getOperand() or
233-
result = this.(PreDecrExpr).getOperand() or
234-
result = this.(UnaryMinusExpr).getOperand() or
235-
result = this.(ComplementExpr).getOperand()
236-
}
237-
238-
/** Returns the operation representing this expression. */
239-
TUnarySignOperation getOp() {
240-
this instanceof PreIncrExpr and result = TIncOp()
241-
or
242-
this instanceof PreDecrExpr and result = TDecOp()
243-
or
244-
this instanceof UnaryMinusExpr and result = TNegOp()
245-
or
246-
this instanceof ComplementExpr and result = TBitNotOp()
247-
}
248-
}
249-
250-
/** Class to represent binary expressions. */
251-
class BinaryExpr extends Expr {
252-
BinaryExpr() {
253-
this instanceof AddExpr or
254-
this instanceof SubExpr or
255-
this instanceof MulExpr or
256-
this instanceof DivExpr or
257-
this instanceof RemExpr or
258-
this instanceof BitwiseAndExpr or
259-
this instanceof BitwiseOrExpr or
260-
this instanceof BitwiseXorExpr or
261-
this instanceof LShiftExpr or
262-
this instanceof RShiftExpr
263-
}
264-
265-
/** Returns the operation representing this expression. */
266-
TBinarySignOperation getOp() {
267-
this instanceof AddExpr and result = TAddOp()
268-
or
269-
this instanceof SubExpr and result = TSubOp()
270-
or
271-
this instanceof MulExpr and result = TMulOp()
272-
or
273-
this instanceof DivExpr and result = TDivOp()
274-
or
275-
this instanceof RemExpr and result = TRemOp()
276-
or
277-
this instanceof BitwiseAndExpr and result = TBitAndOp()
278-
or
279-
this instanceof BitwiseOrExpr and result = TBitOrOp()
280-
or
281-
this instanceof BitwiseXorExpr and result = TBitXorOp()
282-
or
283-
this instanceof LShiftExpr and result = TLShiftOp()
284-
or
285-
this instanceof RShiftExpr and result = TRShiftOp()
286-
}
287-
}
288-
289288
Expr getARead(Ssa::Definition v) { result = v.getARead() }
290289

291290
Field getField(FieldAccess fa) { result = fa.getTarget() }

java/ql/src/semmle/code/java/dataflow/internal/rangeanalysis/SignAnalysisCommon.qll

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,21 +328,20 @@ Sign exprSign(Expr e) {
328328

329329
/** Gets a possible sign for `e` from the signs of its child nodes. */
330330
private Sign specificSubExprSign(Expr e) {
331-
result = exprSign(getASubExpr(e))
331+
result = exprSign(getASubExprWithSameSign(e))
332332
or
333-
e =
334-
any(DivExpr div |
335-
result = exprSign(div.getLeftOperand()) and
336-
result != TZero() and
337-
div.getRightOperand().(RealLiteral).getValue().toFloat() = 0
338-
)
333+
exists(DivExpr div | div = e |
334+
result = exprSign(div.getLeftOperand()) and
335+
result != TZero() and
336+
div.getRightOperand().(RealLiteral).getValue().toFloat() = 0
337+
)
339338
or
340-
exists(UnaryExpr unary | unary = e |
339+
exists(UnaryOperation unary | unary = e |
341340
result = exprSign(unary.getOperand()).applyUnaryOp(unary.getOp())
342341
)
343342
or
344343
exists(Sign s1, Sign s2 | binaryOpSigns(e, s1, s2) |
345-
result = s1.applyBinaryOp(s2, e.(BinaryExpr).getOp())
344+
result = s1.applyBinaryOp(s2, e.(BinaryOperation).getOp())
346345
)
347346
}
348347

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,6 @@ module Private {
4545

4646
class DivExpr = J::DivExpr;
4747

48-
class BinaryOperation extends J::Expr {
49-
BinaryOperation() {
50-
this instanceof J::BinaryExpr or
51-
this instanceof J::AssignOp
52-
}
53-
54-
Expr getLeftOperand() {
55-
result = this.(J::BinaryExpr).getLeftOperand() or result = this.(J::AssignOp).getDest()
56-
}
57-
58-
Expr getRightOperand() {
59-
result = this.(J::BinaryExpr).getRightOperand() or result = this.(J::AssignOp).getRhs()
60-
}
61-
}
62-
6348
/** Class to represent float and double literals. */
6449
class RealLiteral extends J::Literal {
6550
RealLiteral() {
@@ -68,9 +53,9 @@ module Private {
6853
}
6954
}
7055

71-
/** Class to represent unary expressions. */
72-
class UnaryExpr extends J::Expr {
73-
UnaryExpr() {
56+
/** Class to represent unary operation. */
57+
class UnaryOperation extends J::Expr {
58+
UnaryOperation() {
7459
this instanceof J::PreIncExpr or
7560
this instanceof J::PreDecExpr or
7661
this instanceof J::MinusExpr or
@@ -97,9 +82,9 @@ module Private {
9782
}
9883
}
9984

100-
/** Class to represent binary expressions. */
101-
class BinaryExpr extends J::Expr {
102-
BinaryExpr() {
85+
/** Class to represent binary operation. */
86+
class BinaryOperation extends J::Expr {
87+
BinaryOperation() {
10388
this instanceof J::AddExpr or
10489
this instanceof J::AssignAddExpr or
10590
this instanceof J::SubExpr or
@@ -170,6 +155,14 @@ module Private {
170155
or
171156
this instanceof J::AssignURShiftExpr and result = TURShiftOp()
172157
}
158+
159+
Expr getLeftOperand() {
160+
result = this.(J::BinaryExpr).getLeftOperand() or result = this.(J::AssignOp).getDest()
161+
}
162+
163+
Expr getRightOperand() {
164+
result = this.(J::BinaryExpr).getRightOperand() or result = this.(J::AssignOp).getRhs()
165+
}
173166
}
174167

175168
predicate ssaRead = RU::ssaRead/2;
@@ -300,7 +293,7 @@ private module Impl {
300293
}
301294

302295
/** Returns a sub expression of `e` for expression types where the sign depends on the child. */
303-
Expr getASubExpr(Expr e) {
296+
Expr getASubExprWithSameSign(Expr e) {
304297
result = e.(AssignExpr).getSource() or
305298
result = e.(PlusExpr).getExpr() or
306299
result = e.(PostIncExpr).getExpr() or

java/ql/test/library-tests/dataflow/sign-analysis/SignAnalysis.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ string getASignString(Expr e) {
1818
}
1919

2020
from Expr e
21-
where not e instanceof Element or e.(Element).fromSource()
21+
where e.getEnclosingCallable().fromSource()
2222
select e, strictconcat(string s | s = getASignString(e) | s, " ")

0 commit comments

Comments
 (0)