Skip to content

Commit 760f803

Browse files
committed
C++: Add a new test to test assignment certainty (i.e., whether the entire buffer is overwritten).
1 parent 3a21f3c commit 760f803

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
void use(...);
2+
3+
void test1() {
4+
int x = 0; // $ certain="SSA def(&x)" certain="SSA def(x)"
5+
use(x);
6+
7+
x = 1; // $ certain="SSA def(x)"
8+
use(x);
9+
10+
int* p = &x; // $ certain="SSA def(&p)" certain="SSA def(p)" certain="SSA def(*p)"
11+
use(p);
12+
13+
*p = 2; // $ certain="SSA def(*p)"
14+
use(p);
15+
16+
p = nullptr; // $ certain="SSA def(p)" certain="SSA def(*p)"
17+
use(p);
18+
19+
*p = 2; // $ uncertain="SSA def(*p)"
20+
use(p);
21+
}
22+
23+
void test2(bool b) { // $ certain="SSA def(&b)" certain="SSA def(b)"
24+
{
25+
int x; // $ certain="SSA def(&x)"
26+
if(b) {
27+
x = 0; // $ certain="SSA def(x)"
28+
} else {
29+
x = 1; // $ certain="SSA def(x)"
30+
}
31+
use(x); // $ uncertain="SSA phi(x)"
32+
}
33+
34+
{
35+
int x; // $ certain="SSA def(&x)" certain="SSA def(x)"
36+
if(b) {
37+
x = 0; // $ certain="SSA def(x)"
38+
} else {
39+
40+
}
41+
use(x); // $ uncertain="SSA phi(x)"
42+
}
43+
44+
{
45+
int x; // $ certain="SSA def(&x)" certain="SSA def(x)"
46+
int* p = &x; // $ certain="SSA def(&p)" certain="SSA def(p)" certain="SSA def(*p)"
47+
if(b) {
48+
*p = 0; // $ certain="SSA def(*p)"
49+
} else {
50+
*(p + 1) = 1; // $ uncertain="SSA def(*p)"
51+
}
52+
use(p); // $ uncertain="SSA phi(*p)"
53+
}
54+
55+
}
56+
57+
void test3(bool b) { // $ certain="SSA def(&b)" certain="SSA def(b)"
58+
for(int i = 0; i < 10;) { // $ certain="SSA def(&i)" certain="SSA def(i)" uncertain="SSA phi(i)"
59+
if(b) {
60+
++i; // $ certain="SSA def(i)"
61+
}
62+
use(i); // $ uncertain="SSA phi(i)"
63+
}
64+
}

cpp/ql/test/library-tests/dataflow/certain/test.expected

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import cpp
2+
import utils.test.InlineExpectationsTest
3+
import semmle.code.cpp.dataflow.new.DataFlow::DataFlow
4+
5+
bindingset[s]
6+
string quote(string s) { if s.matches("% %") then result = "\"" + s + "\"" else result = s }
7+
8+
module AsDefinitionTest implements TestSig {
9+
string getARelevantTag() { result = ["certain", "uncertain"] }
10+
11+
predicate hasActualResult(Location location, string element, string tag, string value) {
12+
exists(Ssa::Definition d |
13+
location = d.getLocation() and
14+
element = d.toString() and
15+
value = quote(d.toString())
16+
|
17+
if d.isCertain() then tag = "certain" else tag = "uncertain"
18+
)
19+
}
20+
}
21+
22+
import MakeTest<AsDefinitionTest>

0 commit comments

Comments
 (0)