Skip to content

Commit ca1437a

Browse files
committed
Rust: Move the getGreaterOperand/getLesserOperand predicates into RelationalOperation.
1 parent bc4b69b commit ca1437a

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

rust/ql/lib/codeql/rust/elements/ComparisonOperation.qll

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,23 @@ final class NotEqualOperation extends EqualityOperationImpl {
3333
/**
3434
* A relational comparison operation, that is, one of `<=`, `<`, `>`, or `>=`.
3535
*/
36-
abstract private class RelationalOperationImpl extends BinaryExpr, ComparisonOperationImpl { }
36+
abstract private class RelationalOperationImpl extends BinaryExpr, ComparisonOperationImpl {
37+
/**
38+
* Gets the operand on the "greater" (or "greater-or-equal") side
39+
* of this relational expression, that is, the side that is larger
40+
* if the overall expression evaluates to `true`; for example on
41+
* `x <= 20` this is the `20`, and on `y > 0` it is `y`.
42+
*/
43+
abstract Expr getGreaterOperand();
44+
45+
/**
46+
* Gets the operand on the "lesser" (or "lesser-or-equal") side
47+
* of this relational expression, that is, the side that is smaller
48+
* if the overall expression evaluates to `true`; for example on
49+
* `x <= 20` this is `x`, and on `y > 0` it is the `0`.
50+
*/
51+
abstract Expr getLesserOperand();
52+
}
3753

3854
final class RelationalOperation = RelationalOperationImpl;
3955

@@ -42,25 +58,41 @@ final class RelationalOperation = RelationalOperationImpl;
4258
*/
4359
final class LessThanOperation extends RelationalOperationImpl, BinaryExpr {
4460
LessThanOperation() { this.getOperatorName() = "<" }
61+
62+
override Expr getGreaterOperand() { result = this.getRhs() }
63+
64+
override Expr getLesserOperand() { result = this.getLhs() }
4565
}
4666

4767
/**
48-
* The greater than comparison operation, `>?`.
68+
* The greater than comparison operation, `>`.
4969
*/
5070
final class GreaterThanOperation extends RelationalOperationImpl, BinaryExpr {
5171
GreaterThanOperation() { this.getOperatorName() = ">" }
72+
73+
override Expr getGreaterOperand() { result = this.getLhs() }
74+
75+
override Expr getLesserOperand() { result = this.getRhs() }
5276
}
5377

5478
/**
5579
* The less than or equal comparison operation, `<=`.
5680
*/
5781
final class LessOrEqualOperation extends RelationalOperationImpl, BinaryExpr {
5882
LessOrEqualOperation() { this.getOperatorName() = "<=" }
83+
84+
override Expr getGreaterOperand() { result = this.getRhs() }
85+
86+
override Expr getLesserOperand() { result = this.getLhs() }
5987
}
6088

6189
/**
6290
* The less than or equal comparison operation, `>=`.
6391
*/
6492
final class GreaterOrEqualOperation extends RelationalOperationImpl, BinaryExpr {
6593
GreaterOrEqualOperation() { this.getOperatorName() = ">=" }
94+
95+
override Expr getGreaterOperand() { result = this.getLhs() }
96+
97+
override Expr getLesserOperand() { result = this.getRhs() }
6698
}

rust/ql/lib/codeql/rust/security/UncontrolledAllocationSizeExtensions.qll

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,16 @@ module UncontrolledAllocationSize {
4343
}
4444
}
4545

46-
/**
47-
* Gets the operand on the "greater" (or "greater-or-equal") side
48-
* of this relational expression, that is, the side that is larger
49-
* if the overall expression evaluates to `true`; for example on
50-
* `x <= 20` this is the `20`, and on `y > 0` it is `y`.
51-
*/
52-
private Expr getGreaterOperand(BinaryExpr op) {
53-
op.getOperatorName() = ["<", "<="] and
54-
result = op.getRhs()
55-
or
56-
op.getOperatorName() = [">", ">="] and
57-
result = op.getLhs()
58-
}
59-
60-
/**
61-
* Gets the operand on the "lesser" (or "lesser-or-equal") side
62-
* of this relational expression, that is, the side that is smaller
63-
* if the overall expression evaluates to `true`; for example on
64-
* `x <= 20` this is `x`, and on `y > 0` it is the `0`.
65-
*/
66-
private Expr getLesserOperand(BinaryExpr op) {
67-
op.getOperatorName() = ["<", "<="] and
68-
result = op.getLhs()
69-
or
70-
op.getOperatorName() = [">", ">="] and
71-
result = op.getRhs()
72-
}
73-
7446
/**
7547
* Holds if comparison `g` having result `branch` indicates an upper bound for the sub-expression
7648
* `node`. For example when the comparison `x < 10` is true, we have an upper bound for `x`.
7749
*/
7850
private predicate isUpperBoundCheck(CfgNodes::AstCfgNode g, Cfg::CfgNode node, boolean branch) {
7951
exists(BinaryExpr cmp | g = cmp.getACfgNode() |
80-
node = getLesserOperand(cmp).getACfgNode() and
52+
node = cmp.(RelationalOperation).getLesserOperand().getACfgNode() and
8153
branch = true
8254
or
83-
node = getGreaterOperand(cmp).getACfgNode() and
55+
node = cmp.(RelationalOperation).getGreaterOperand().getACfgNode() and
8456
branch = false
8557
or
8658
cmp.getOperatorName() = "==" and

rust/ql/test/library-tests/operations/Operations.ql

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ string describe(Expr op) {
3434
}
3535

3636
module OperationsTest implements TestSig {
37-
string getARelevantTag() { result = describe(_) or result = ["Op", "Operands"] }
37+
string getARelevantTag() {
38+
result = describe(_) or result = ["Op", "Operands", "Greater", "Lesser"]
39+
}
3840

3941
predicate hasActualResult(Location location, string element, string tag, string value) {
4042
exists(Expr op |
@@ -51,6 +53,14 @@ module OperationsTest implements TestSig {
5153
op instanceof Operation and
5254
tag = "Operands" and
5355
value = count(op.(Operation).getAnOperand()).toString()
56+
or
57+
op instanceof RelationalOperation and
58+
tag = "Greater" and
59+
value = op.(RelationalOperation).getGreaterOperand().toString()
60+
or
61+
op instanceof RelationalOperation and
62+
tag = "Lesser" and
63+
value = op.(RelationalOperation).getLesserOperand().toString()
5464
)
5565
)
5666
}

rust/ql/test/library-tests/operations/test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ fn test_operations(
1313
// comparison operations
1414
x == y; // $ Operation Op=== Operands=2 BinaryExpr ComparisonOperation EqualityOperation EqualOperation
1515
x != y; // $ Operation Op=!= Operands=2 BinaryExpr ComparisonOperation EqualityOperation NotEqualOperation
16-
x < y; // $ Operation Op=< Operands=2 BinaryExpr ComparisonOperation RelationalOperation LessThanOperation
17-
x <= y; // $ Operation Op=<= Operands=2 BinaryExpr ComparisonOperation RelationalOperation LessOrEqualOperation
18-
x > y; // $ Operation Op=> Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterThanOperation
19-
x >= y; // $ Operation Op=>= Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterOrEqualOperation
16+
x < y; // $ Operation Op=< Operands=2 BinaryExpr ComparisonOperation RelationalOperation LessThanOperation Greater=y Lesser=x
17+
x <= y; // $ Operation Op=<= Operands=2 BinaryExpr ComparisonOperation RelationalOperation LessOrEqualOperation Greater=y Lesser=x
18+
x > y; // $ Operation Op=> Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterThanOperation Greater=x Lesser=y
19+
x >= y; // $ Operation Op=>= Operands=2 BinaryExpr ComparisonOperation RelationalOperation GreaterOrEqualOperation Greater=x Lesser=y
2020

2121
// arithmetic operations
2222
x + y; // $ Operation Op=+ Operands=2 BinaryExpr

0 commit comments

Comments
 (0)