|
10 | 10 | */
|
11 | 11 |
|
12 | 12 | import python
|
| 13 | +import semmle.python.dataflow.new.DataFlow |
| 14 | +import semmle.python.ApiGraphs |
13 | 15 |
|
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 | + ) |
20 | 20 | }
|
21 | 21 |
|
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() |
27 | 25 | )
|
28 | 26 | }
|
29 | 27 |
|
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()] } |
37 | 29 |
|
38 |
| -from Call c, BinaryExpr s |
| 30 | +from DataFlow::CallCfgNode c, BinaryExpr s, DataFlow::ExprNode left, DataFlow::ExprNode right |
39 | 31 | 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 |
42 | 34 | 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() |
45 | 39 | select c, "Pythagorean calculation with sub-optimal numerics"
|
0 commit comments