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

Skip to content

Commit 00fa803

Browse files
committed
Python (pruning): Fix up handling of integer inequality.
1 parent a3d50e8 commit 00fa803

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

python/ql/src/semmle/python/Pruning.qll

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,7 @@ module Pruner {
162162
TIsNone(boolean b) { b = true or b = false }
163163
or
164164
TConstrainedByConstant(CompareOp op, int k) {
165-
exists(Compare comp, Cmpop cop, IntegerLiteral l |
166-
comp.compares(_, cop, l) and
167-
l.getValue() = k and
168-
op.forOp(cop)
169-
)
165+
int_test(_, _, op, k)
170166
or
171167
exists(Assign a | a.getValue().(IntegerLiteral).getValue() = k) and op = eq()
172168
}
@@ -416,7 +412,7 @@ module Pruner {
416412
)
417413
}
418414

419-
private Constraint constraintFromTest(SsaVariable var, UnprunedCfgNode node) {
415+
Constraint constraintFromTest(SsaVariable var, UnprunedCfgNode node) {
420416
py_ssa_use(node, var) and result = TTruthy(true)
421417
or
422418
exists(boolean b |
@@ -443,13 +439,20 @@ module Pruner {
443439
)
444440
}
445441

446-
predicate int_test(UnprunedCompareNode test, SsaVariable var, CompareOp op, int k) {
442+
predicate int_test(UnprunedCfgNode test, SsaVariable var, CompareOp op, int k) {
447443
exists(UnprunedCfgNode left, UnprunedCfgNode right, Cmpop cop |
444+
test.(UnprunedCompareNode).operands(left, cop, right)
445+
|
446+
op.forOp(cop) and
448447
py_ssa_use(left, var) and
449-
test.operands(left, cop, right) and
450-
right.getNode().(IntegerLiteral).getValue() = k and
451-
op.forOp(cop)
448+
right.getNode().(IntegerLiteral).getValue() = k
449+
or
450+
op.reverse().forOp(cop) and
451+
py_ssa_use(right, var) and
452+
left.getNode().(IntegerLiteral).getValue() = k
452453
)
454+
or
455+
int_test(test.(UnprunedNot).getOperand(), var, op.invert(), k)
453456
}
454457

455458
predicate int_assignment(UnprunedCfgNode asgn, SsaVariable var, CompareOp op, int k) {

python/ql/test/library-tests/ControlFlow/pruning/test.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@
5050
| 186 | 1 |
5151
| 189 | 1 |
5252
| 192 | 1 |
53+
| 198 | 0 |
54+
| 204 | 0 |
55+
| 210 | 0 |

python/ql/test/library-tests/ControlFlow/pruning/test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,22 @@ def func2():
190190
except IOError:
191191
true12 = 0
192192
count
193+
194+
def inequality1(x):
195+
if x < 4:
196+
return
197+
if x < 4:
198+
count
199+
200+
def inequality2(x):
201+
if x < 4:
202+
return
203+
if not x >= 4:
204+
count
205+
206+
def reversed_inequality(x):
207+
if x < 4:
208+
return
209+
if 4 > x:
210+
count
211+

0 commit comments

Comments
 (0)