Skip to content

Commit 9d77446

Browse files
authored
Merge pull request #8859 from tausbn/python-fix-bad-essa-joins
Python: Fix a bunch of bad joins
2 parents 5974248 + 7d73695 commit 9d77446

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

python/ql/lib/semmle/python/essa/Essa.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,13 @@ private EssaVariable potential_input(EssaNodeRefinement ref) {
498498

499499
/** An assignment to a variable `v = val` */
500500
class AssignmentDefinition extends EssaNodeDefinition {
501+
ControlFlowNode value;
502+
501503
AssignmentDefinition() {
502-
SsaSource::assignment_definition(this.getSourceVariable(), this.getDefiningNode(), _)
504+
SsaSource::assignment_definition(this.getSourceVariable(), this.getDefiningNode(), value)
503505
}
504506

505-
ControlFlowNode getValue() {
506-
SsaSource::assignment_definition(this.getSourceVariable(), this.getDefiningNode(), result)
507-
}
507+
ControlFlowNode getValue() { result = value }
508508

509509
override string getRepresentation() { result = this.getValue().getNode().toString() }
510510

python/ql/lib/semmle/python/essa/SsaCompute.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,8 @@ private module SsaComputeImpl {
496496
predicate firstUse(EssaDefinition def, ControlFlowNode use) {
497497
exists(SsaSourceVariable v, BasicBlock b1, int i1, BasicBlock b2, int i2 |
498498
adjacentVarRefs(v, b1, i1, b2, i2) and
499-
definesAt(def, v, b1, i1) and
500-
variableSourceUse(v, use, b2, i2)
499+
definesAt(def, pragma[only_bind_into](v), b1, i1) and
500+
variableSourceUse(pragma[only_bind_into](v), use, b2, i2)
501501
)
502502
or
503503
exists(

python/ql/src/Numerics/Pythagorean.ql

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,30 @@
1010
*/
1111

1212
import python
13+
import semmle.python.dataflow.new.DataFlow
14+
import semmle.python.ApiGraphs
1315

14-
predicate squareOp(BinaryExpr e) {
15-
e.getOp() instanceof Pow and e.getRight().(IntegerLiteral).getN() = "2"
16-
}
17-
18-
predicate squareMul(BinaryExpr e) {
19-
e.getOp() instanceof Mult and e.getRight().(Name).getId() = e.getLeft().(Name).getId()
16+
DataFlow::ExprNode squareOp() {
17+
exists(BinaryExpr e | e = result.asExpr() |
18+
e.getOp() instanceof Pow and e.getRight().(IntegerLiteral).getN() = "2"
19+
)
2020
}
2121

22-
predicate squareRef(Name e) {
23-
e.isUse() and
24-
exists(SsaVariable v, Expr s | v.getVariable() = e.getVariable() |
25-
s = v.getDefinition().getNode().getParentNode().(AssignStmt).getValue() and
26-
square(s)
22+
DataFlow::ExprNode squareMul() {
23+
exists(BinaryExpr e | e = result.asExpr() |
24+
e.getOp() instanceof Mult and e.getRight().(Name).getId() = e.getLeft().(Name).getId()
2725
)
2826
}
2927

30-
predicate square(Expr e) {
31-
squareOp(e)
32-
or
33-
squareMul(e)
34-
or
35-
squareRef(e)
36-
}
28+
DataFlow::ExprNode square() { result in [squareOp(), squareMul()] }
3729

38-
from Call c, BinaryExpr s
30+
from DataFlow::CallCfgNode c, BinaryExpr s, DataFlow::ExprNode left, DataFlow::ExprNode right
3931
where
40-
c.getFunc().toString() = "sqrt" and
41-
c.getArg(0) = s and
32+
c = API::moduleImport("math").getMember("sqrt").getACall() and
33+
c.getArg(0).asExpr() = s and
4234
s.getOp() instanceof Add and
43-
square(s.getLeft()) and
44-
square(s.getRight())
35+
left.asExpr() = s.getLeft() and
36+
right.asExpr() = s.getRight() and
37+
left.getALocalSource() = square() and
38+
right.getALocalSource() = square()
4539
select c, "Pythagorean calculation with sub-optimal numerics"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| pythagorean_test.py:6:12:6:28 | sqrt() | Pythagorean calculation with sub-optimal numerics |
2-
| pythagorean_test.py:9:12:9:26 | sqrt() | Pythagorean calculation with sub-optimal numerics |
3-
| pythagorean_test.py:14:12:14:24 | sqrt() | Pythagorean calculation with sub-optimal numerics |
1+
| pythagorean_test.py:6:12:6:28 | ControlFlowNode for sqrt() | Pythagorean calculation with sub-optimal numerics |
2+
| pythagorean_test.py:9:12:9:26 | ControlFlowNode for sqrt() | Pythagorean calculation with sub-optimal numerics |
3+
| pythagorean_test.py:14:12:14:24 | ControlFlowNode for sqrt() | Pythagorean calculation with sub-optimal numerics |

0 commit comments

Comments
 (0)