Skip to content

Commit cabcb83

Browse files
authored
Merge pull request #21212 from MathiasVP/fix-as-definition
C++: Fix missing results for `Node.asDefinition`
2 parents e360800 + 6c2a3a6 commit cabcb83

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* Fixed a bug which caused `Node.asDefinition()` to not have a result for certain assignments.

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ class Node extends TIRDataFlowNode {
312312
*/
313313
Expr asDefinition() { result = this.asDefinition(_) }
314314

315+
private predicate isCertainStore() {
316+
exists(SsaImpl::Definition def |
317+
SsaImpl::defToNode(this, def, _) and
318+
def.isCertain()
319+
)
320+
}
321+
315322
/**
316323
* Gets the definition associated with this node, if any.
317324
*
@@ -361,11 +368,10 @@ class Node extends TIRDataFlowNode {
361368
* pointed to by `p`.
362369
*/
363370
Expr asDefinition(boolean uncertain) {
364-
exists(StoreInstruction store, SsaImpl::Definition def |
371+
exists(StoreInstruction store |
365372
store = this.asInstruction() and
366373
result = asDefinitionImpl(store) and
367-
SsaImpl::defToNode(this, def, _) and
368-
if def.isCertain() then uncertain = false else uncertain = true
374+
if this.isCertainStore() then uncertain = false else uncertain = true
369375
)
370376
}
371377

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct S {
2+
int x;
3+
};
4+
5+
void use(int);
6+
7+
void test() {
8+
int y = 43; // $ asDefinition=43
9+
use(y);
10+
y = 44; // $ asDefinition="... = ..."
11+
use(y);
12+
13+
int x = 43; // $ asDefinition=43
14+
x = 44; // $ asDefinition="... = ..."
15+
16+
S s;
17+
s.x = 42; // $ asDefinition="... = ..."
18+
}

cpp/ql/test/library-tests/dataflow/asDefinition/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 = "asDefinition" }
10+
11+
predicate hasActualResult(Location location, string element, string tag, string value) {
12+
exists(Node n, Expr e |
13+
e = n.asDefinition() and
14+
location = e.getLocation() and
15+
element = n.toString() and
16+
tag = "asDefinition" and
17+
value = quote(e.toString())
18+
)
19+
}
20+
}
21+
22+
import MakeTest<AsDefinitionTest>

0 commit comments

Comments
 (0)