Skip to content

Commit d4e3f7f

Browse files
committed
C++: fix missing bounds in new range analysis
1 parent b941d54 commit d4e3f7f

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

cpp/ql/lib/experimental/semmle/code/cpp/semantic/SemanticBound.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
private import SemanticExpr
66
private import SemanticExprSpecific::SemanticExprConfig as Specific
77
private import SemanticSSA
8+
private import semmle.code.cpp.Location // TODO: SemLocation?
89

910
/**
1011
* A valid base for an expression bound.
@@ -14,6 +15,8 @@ private import SemanticSSA
1415
class SemBound instanceof Specific::Bound {
1516
final string toString() { result = super.toString() }
1617

18+
final Location getLocation() {result = super.getLocation() }
19+
1720
final SemExpr getExpr(int delta) { result = Specific::getBoundExpr(this, delta) }
1821
}
1922

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
private import RangeAnalysisStage
2+
3+
module IntDelta implements DeltaSig {
4+
class Delta = int;
5+
6+
bindingset[d]
7+
bindingset[result]
8+
float toFloat(Delta d) { result = d }
9+
10+
bindingset[d]
11+
bindingset[result]
12+
int toInt(Delta d) { result = d }
13+
14+
bindingset[n]
15+
bindingset[result]
16+
Delta fromInt(int n) { result = n }
17+
18+
bindingset[f]
19+
Delta fromFloat(float f) {
20+
result =
21+
min(float diff, float res |
22+
diff = (res - f) and res = f.ceil()
23+
or
24+
diff = (f - res) and res = f.floor()
25+
|
26+
res order by diff
27+
)
28+
}
29+
}

cpp/ql/lib/experimental/semmle/code/cpp/semantic/analysis/RangeAnalysisImpl.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ private import experimental.semmle.code.cpp.semantic.analysis.FloatDelta
44
private import RangeUtils
55
private import experimental.semmle.code.cpp.semantic.SemanticBound as SemanticBound
66
private import semmle.code.cpp.ir.IR as IR
7+
private import semmle.code.cpp.Location // TODO: SemLocation?
78

89
private module ConstantBounds implements BoundSig<FloatDelta> {
910
class SemBound instanceof SemanticBound::SemBound {
@@ -15,6 +16,8 @@ private module ConstantBounds implements BoundSig<FloatDelta> {
1516

1617
string toString() { result = super.toString() }
1718

19+
Location getLocation() { result = super.getLocation() }
20+
1821
SemExpr getExpr(float delta) { result = super.getExpr(delta) }
1922
}
2023

@@ -26,9 +29,11 @@ private module ConstantBounds implements BoundSig<FloatDelta> {
2629
}
2730

2831
private module RelativeBounds implements BoundSig<FloatDelta> {
29-
class SemBound instanceof SemanticBound::SemSsaBound {
32+
class SemBound instanceof SemanticBound::SemBound {
3033
string toString() { result = super.toString() }
3134

35+
Location getLocation() { result = super.getLocation() }
36+
3237
SemExpr getExpr(float delta) { result = super.getExpr(delta) }
3338
}
3439

cpp/ql/lib/experimental/semmle/code/cpp/semantic/analysis/RangeAnalysisStage.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import experimental.semmle.code.cpp.semantic.SemanticCFG
7373
import experimental.semmle.code.cpp.semantic.SemanticType
7474
import experimental.semmle.code.cpp.semantic.SemanticOpcode
7575
private import ConstantAnalysis
76+
private import semmle.code.cpp.Location
7677

7778
/**
7879
* Holds if `typ` is a small integral type with the given lower and upper bounds.
@@ -228,6 +229,8 @@ signature module UtilSig<DeltaSig DeltaParam> {
228229

229230
signature module BoundSig<DeltaSig D> {
230231
class SemBound {
232+
string toString();
233+
Location getLocation();
231234
SemExpr getExpr(D::Delta delta);
232235
}
233236

0 commit comments

Comments
 (0)