mirror of
https://github.com/github/codeql.git
synced 2026-05-24 08:07:07 +02:00
C++: Add a new test to test assignment certainty (i.e., whether the entire buffer is overwritten).
This commit is contained in:
64
cpp/ql/test/library-tests/dataflow/certain/test.cpp
Normal file
64
cpp/ql/test/library-tests/dataflow/certain/test.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
void use(...);
|
||||
|
||||
void test1() {
|
||||
int x = 0; // $ certain="SSA def(&x)" certain="SSA def(x)"
|
||||
use(x);
|
||||
|
||||
x = 1; // $ certain="SSA def(x)"
|
||||
use(x);
|
||||
|
||||
int* p = &x; // $ certain="SSA def(&p)" certain="SSA def(p)" certain="SSA def(*p)"
|
||||
use(p);
|
||||
|
||||
*p = 2; // $ certain="SSA def(*p)"
|
||||
use(p);
|
||||
|
||||
p = nullptr; // $ certain="SSA def(p)" certain="SSA def(*p)"
|
||||
use(p);
|
||||
|
||||
*p = 2; // $ uncertain="SSA def(*p)"
|
||||
use(p);
|
||||
}
|
||||
|
||||
void test2(bool b) { // $ certain="SSA def(&b)" certain="SSA def(b)"
|
||||
{
|
||||
int x; // $ certain="SSA def(&x)"
|
||||
if(b) {
|
||||
x = 0; // $ certain="SSA def(x)"
|
||||
} else {
|
||||
x = 1; // $ certain="SSA def(x)"
|
||||
}
|
||||
use(x); // $ uncertain="SSA phi(x)"
|
||||
}
|
||||
|
||||
{
|
||||
int x; // $ certain="SSA def(&x)" certain="SSA def(x)"
|
||||
if(b) {
|
||||
x = 0; // $ certain="SSA def(x)"
|
||||
} else {
|
||||
|
||||
}
|
||||
use(x); // $ uncertain="SSA phi(x)"
|
||||
}
|
||||
|
||||
{
|
||||
int x; // $ certain="SSA def(&x)" certain="SSA def(x)"
|
||||
int* p = &x; // $ certain="SSA def(&p)" certain="SSA def(p)" certain="SSA def(*p)"
|
||||
if(b) {
|
||||
*p = 0; // $ certain="SSA def(*p)"
|
||||
} else {
|
||||
*(p + 1) = 1; // $ uncertain="SSA def(*p)"
|
||||
}
|
||||
use(p); // $ uncertain="SSA phi(*p)"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void test3(bool b) { // $ certain="SSA def(&b)" certain="SSA def(b)"
|
||||
for(int i = 0; i < 10;) { // $ certain="SSA def(&i)" certain="SSA def(i)" uncertain="SSA phi(i)"
|
||||
if(b) {
|
||||
++i; // $ certain="SSA def(i)"
|
||||
}
|
||||
use(i); // $ uncertain="SSA phi(i)"
|
||||
}
|
||||
}
|
||||
22
cpp/ql/test/library-tests/dataflow/certain/test.ql
Normal file
22
cpp/ql/test/library-tests/dataflow/certain/test.ql
Normal file
@@ -0,0 +1,22 @@
|
||||
import cpp
|
||||
import utils.test.InlineExpectationsTest
|
||||
import semmle.code.cpp.dataflow.new.DataFlow::DataFlow
|
||||
|
||||
bindingset[s]
|
||||
string quote(string s) { if s.matches("% %") then result = "\"" + s + "\"" else result = s }
|
||||
|
||||
module AsDefinitionTest implements TestSig {
|
||||
string getARelevantTag() { result = ["certain", "uncertain"] }
|
||||
|
||||
predicate hasActualResult(Location location, string element, string tag, string value) {
|
||||
exists(Ssa::Definition d |
|
||||
location = d.getLocation() and
|
||||
element = d.toString() and
|
||||
value = quote(d.toString())
|
||||
|
|
||||
if d.isCertain() then tag = "certain" else tag = "uncertain"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
import MakeTest<AsDefinitionTest>
|
||||
Reference in New Issue
Block a user