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

Skip to content

Commit 6e349eb

Browse files
committed
Python: Make py/side-effect-in-assert handle example
Also removed parantheses
1 parent ae8dbd8 commit 6e349eb

4 files changed

Lines changed: 15 additions & 4 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
assert(subprocess.call(['run-backup']) == 0)
1+
assert subprocess.call(['run-backup']) == 0

python/ql/src/Statements/SideEffectInAssert.ql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,23 @@ predicate func_with_side_effects(Expr e) {
2828
)
2929
}
3030

31+
predicate call_with_side_effect(Call e) {
32+
e.getAFlowNode() = Value::named("subprocess.call").getACall()
33+
or
34+
e.getAFlowNode() = Value::named("subprocess.check_call").getACall()
35+
or
36+
e.getAFlowNode() = Value::named("subprocess.check_output").getACall()
37+
}
38+
3139
predicate probable_side_effect(Expr e) {
3240
// Only consider explicit yields, not artificial ones in comprehensions
3341
e instanceof Yield and not exists(Comp c | c.contains(e))
3442
or
3543
e instanceof YieldFrom
3644
or
3745
e instanceof Call and func_with_side_effects(e.(Call).getFunc())
46+
or
47+
e instanceof Call and call_with_side_effect(e)
3848
}
3949

4050
from Assert a, Expr e
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
| assert.py:5:5:5:20 | Assert | This 'assert' statement contains $@ which may have side effects. | assert.py:5:13:5:19 | Yield | an expression |
22
| assert.py:8:5:8:22 | Assert | This 'assert' statement contains $@ which may have side effects. | assert.py:8:12:8:22 | Attribute() | an expression |
3+
| assert.py:20:1:20:43 | Assert | This 'assert' statement contains $@ which may have side effects. | assert.py:20:8:20:38 | Attribute() | an expression |

python/ql/test/query-tests/Statements/asserts/assert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def assert_tuple(x, y):
1616
assert ()
1717
assert (x, y)
1818

19-
20-
19+
import subprocess
20+
assert subprocess.call(['run-backup']) == 0 # TODO: FN
2121

2222

2323

@@ -103,4 +103,4 @@ def error_assert_in_intermediate_branch(x):
103103
elif yks(x):
104104
pass
105105
else:
106-
pass
106+
pass

0 commit comments

Comments
 (0)