Skip to content

Commit 024bd27

Browse files
authored
Merge pull request #7578 from MathiasVP/store-dest-should-not-be-use
C++: Store destinations should not be uses for dataflow SSA
2 parents 1912c56 + e1598ab commit 024bd27

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,16 @@ private class ReturnParameterIndirection extends Use, TReturnParamIndirection {
170170
}
171171

172172
private predicate isExplicitUse(Operand op) {
173-
op.getDef() instanceof VariableAddressInstruction and
174-
not exists(LoadInstruction load |
175-
load.getSourceAddressOperand() = op and
176-
load.getAUse().getUse() instanceof InitializeIndirectionInstruction
173+
exists(VariableAddressInstruction vai | vai = op.getDef() |
174+
// Don't include this operand as a use if it only exists to initialize the
175+
// indirection of a parameter.
176+
not exists(LoadInstruction load |
177+
load.getSourceAddressOperand() = op and
178+
load.getAUse().getUse() instanceof InitializeIndirectionInstruction
179+
) and
180+
// Don't include this operand as a use if the only use of the address is for a write
181+
// that definitely overrides a variable.
182+
not (explicitWrite(true, _, vai) and exists(unique( | | vai.getAUse())))
177183
)
178184
}
179185

cpp/ql/test/library-tests/dataflow/dataflow-tests/dataflow-ir-consistency.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ postWithInFlow
570570
| test.cpp:481:24:481:30 | FieldAddress [post update] | PostUpdateNode should not be the target of local flow. |
571571
| test.cpp:481:24:481:30 | content [post update] | PostUpdateNode should not be the target of local flow. |
572572
| test.cpp:482:8:482:16 | VariableAddress [post update] | PostUpdateNode should not be the target of local flow. |
573+
| test.cpp:489:7:489:7 | VariableAddress [post update] | PostUpdateNode should not be the target of local flow. |
574+
| test.cpp:491:5:491:5 | x [post update] | PostUpdateNode should not be the target of local flow. |
575+
| test.cpp:494:5:494:5 | x [post update] | PostUpdateNode should not be the target of local flow. |
573576
| true_upon_entry.cpp:9:7:9:7 | VariableAddress [post update] | PostUpdateNode should not be the target of local flow. |
574577
| true_upon_entry.cpp:10:12:10:12 | VariableAddress [post update] | PostUpdateNode should not be the target of local flow. |
575578
| true_upon_entry.cpp:10:27:10:27 | VariableAddress [post update] | PostUpdateNode should not be the target of local flow. |

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,16 @@ void local_field_flow_def_by_ref_steps_with_local_flow(MyStruct * s) {
481481
writes_to_content(s->content);
482482
int* p_content = s->content;
483483
sink(*p_content);
484-
}
484+
}
485+
486+
bool unknown();
487+
488+
void regression_with_phi_flow(int clean1) {
489+
int x = 0;
490+
while (unknown()) {
491+
x = clean1;
492+
if (unknown()) { }
493+
sink(x); // clean
494+
x = source();
495+
}
496+
}

0 commit comments

Comments
 (0)