Skip to content

Commit d67dae8

Browse files
committed
C++: Avoid conflating left and right between operations.
1 parent 9f223f8 commit d67dae8

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,13 @@ private class CompareValueNumber extends ValueNumber {
7272
CompareInstruction getCompareInstruction() { result = cmp }
7373

7474
/**
75-
* Gets a left operand of a `CompareInstruction` that belongs to this
76-
* value number
75+
* Gets the left and right operands of a `CompareInstruction` that
76+
* belongs to this value number.
7777
*/
78-
Operand getLeftOperand() { result = cmp.getLeftOperand() }
79-
80-
/**
81-
* Gets a right operand of a `CompareInstruction` that belongs to this
82-
* value number
83-
*/
84-
Operand getRightOperand() { result = cmp.getRightOperand() }
78+
predicate hasOperands(Operand left, Operand right) {
79+
left = cmp.getLeftOperand() and
80+
right = cmp.getRightOperand()
81+
}
8582
}
8683

8784
private class CompareEQValueNumber extends CompareValueNumber {
@@ -847,15 +844,13 @@ private predicate compares_eq(
847844
private predicate simple_comparison_eq(
848845
CompareValueNumber cmp, Operand left, Operand right, int k, AbstractValue value
849846
) {
850-
left = cmp.getLeftOperand() and
851847
cmp instanceof CompareEQValueNumber and
852-
right = cmp.getRightOperand() and
848+
cmp.hasOperands(left, right) and
853849
k = 0 and
854850
value.(BooleanValue).getValue() = true
855851
or
856-
left = cmp.getLeftOperand() and
857852
cmp instanceof CompareNEValueNumber and
858-
right = cmp.getRightOperand() and
853+
cmp.hasOperands(left, right) and
859854
k = 0 and
860855
value.(BooleanValue).getValue() = false
861856
}
@@ -944,24 +939,20 @@ private predicate compares_ge(
944939

945940
/** Rearrange various simple comparisons into `left < right + k` form. */
946941
private predicate simple_comparison_lt(CompareValueNumber cmp, Operand left, Operand right, int k) {
947-
left = cmp.getLeftOperand() and
942+
cmp.hasOperands(left, right) and
948943
cmp instanceof CompareLTValueNumber and
949-
right = cmp.getRightOperand() and
950944
k = 0
951945
or
952-
left = cmp.getLeftOperand() and
946+
cmp.hasOperands(left, right) and
953947
cmp instanceof CompareLEValueNumber and
954-
right = cmp.getRightOperand() and
955948
k = 1
956949
or
957-
right = cmp.getLeftOperand() and
950+
cmp.hasOperands(right, left) and
958951
cmp instanceof CompareGTValueNumber and
959-
left = cmp.getRightOperand() and
960952
k = 0
961953
or
962-
right = cmp.getLeftOperand() and
954+
cmp.hasOperands(right, left) and
963955
cmp instanceof CompareGEValueNumber and
964-
left = cmp.getRightOperand() and
965956
k = 1
966957
}
967958

0 commit comments

Comments
 (0)