Skip to content

Commit f7273b8

Browse files
committed
C++: Add custom modeling to extensibility.ql
1 parent ee33125 commit f7273b8

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/// Adds its arguments (requires custom modeling in QL)
1+
/// Adds its arguments (has custom modeling in QL)
22
int custom_add_function(int a, int b);
33

44
int test_extensibility_add(int x) {
55
if (x >= -10 && x <= 10) {
66
int result = custom_add_function(x, 100);
7-
return result; // should be 90 .. 110
7+
return result; // 90 .. 110
88
}
99
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
| extensibility.c:5:7:5:7 | x | -2.147483648E9 | 2.147483647E9 |
22
| extensibility.c:5:19:5:19 | x | -10.0 | 2.147483647E9 |
33
| extensibility.c:6:38:6:38 | x | -10.0 | 10.0 |
4-
| extensibility.c:7:12:7:17 | result | -2.147483648E9 | 2.147483647E9 |
4+
| extensibility.c:7:12:7:17 | result | 90.0 | 110.0 |

cpp/ql/test/experimental/library-tests/rangeanalysis/extensibility/extensibility.ql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
22
import experimental.semmle.code.cpp.models.interfaces.SimpleRangeAnalysisExpr
33

4+
class CustomAddFunctionCall extends SimpleRangeAnalysisExpr, FunctionCall {
5+
CustomAddFunctionCall() { this.getTarget().hasGlobalName("custom_add_function") }
6+
7+
override float getLowerBounds() {
8+
exists(float lower0, float lower1 |
9+
lower0 = getFullyConvertedLowerBounds(this.getArgument(0)) and
10+
lower1 = getFullyConvertedLowerBounds(this.getArgument(1)) and
11+
// Note: this rounds toward 0, not -Inf as it should
12+
result = lower0 + lower1
13+
)
14+
}
15+
16+
override float getUpperBounds() {
17+
exists(float upper0, float upper1 |
18+
upper0 = getFullyConvertedUpperBounds(this.getArgument(0)) and
19+
upper1 = getFullyConvertedUpperBounds(this.getArgument(1)) and
20+
// Note: this rounds toward 0, not Inf as it should
21+
result = upper0 + upper1
22+
)
23+
}
24+
25+
override predicate dependsOnChild(Expr child) { child = this.getAnArgument() }
26+
}
27+
428
from VariableAccess expr, float lower, float upper
529
where
630
lower = lowerBound(expr) and

0 commit comments

Comments
 (0)