Skip to content

Commit 9fd8b04

Browse files
committed
C++: Add a SimpleRangeAnalysisDefinition test def
1 parent 826632d commit 9fd8b04

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
| extensibility.c:12:16:12:16 | x | -2.147483648E9 | 2.147483647E9 |
66
| extensibility.c:12:20:12:20 | x | -2.147483648E9 | 2.147483647E9 |
77
| extensibility.c:13:10:13:15 | result | 0.0 | 0.0 |
8-
| extensibility.c:17:3:17:23 | magic_name_at_most_10 | -2.147483648E9 | 2.147483647E9 |
9-
| extensibility.c:18:3:18:23 | magic_name_at_most_20 | -2.147483648E9 | 2.147483647E9 |
8+
| extensibility.c:17:3:17:23 | magic_name_at_most_10 | -2.147483648E9 | 10.0 |
9+
| extensibility.c:18:3:18:23 | magic_name_at_most_20 | -2.147483648E9 | 20.0 |

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
2+
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils // for typeLowerBound
23
import experimental.semmle.code.cpp.models.interfaces.SimpleRangeAnalysisExpr
4+
import experimental.semmle.code.cpp.models.interfaces.SimpleRangeAnalysisDefinition
35

46
class CustomAddFunctionCall extends SimpleRangeAnalysisExpr, FunctionCall {
57
CustomAddFunctionCall() { this.getTarget().hasGlobalName("custom_add_function") }
@@ -37,6 +39,40 @@ class SelfSub extends SimpleRangeAnalysisExpr, SubExpr {
3739
override predicate dependsOnChild(Expr child) { child = this.getAnOperand() }
3840
}
3941

42+
/**
43+
* A definition for test purposes of a parameter `p` that starts with a
44+
* special prefix. This class is written to exploit how QL behaves when class
45+
* fields are not functionally determined by `this`. When multiple parameters
46+
* of the same function have the special prefix, there is still only one
47+
* instance of this class.
48+
*/
49+
class MagicParameterName extends SimpleRangeAnalysisDefinition {
50+
Parameter p;
51+
float value;
52+
53+
MagicParameterName() {
54+
this.definedByParameter(p) and
55+
value = p.getName().regexpCapture("magic_name_at_most_(\\d+)", 1).toFloat()
56+
}
57+
58+
override predicate hasRangeInformationFor(StackVariable v) { v = p }
59+
60+
override predicate dependsOnExpr(StackVariable v, Expr e) {
61+
// No dependencies. This sample class yields constant values.
62+
none()
63+
}
64+
65+
override float getLowerBounds(StackVariable var) {
66+
var = p and
67+
result = typeLowerBound(p.getUnspecifiedType())
68+
}
69+
70+
override float getUpperBounds(StackVariable var) {
71+
var = p and
72+
result = value
73+
}
74+
}
75+
4076
from VariableAccess expr, float lower, float upper
4177
where
4278
lower = lowerBound(expr) and

0 commit comments

Comments
 (0)