Skip to content

Commit f5ebcd2

Browse files
committed
C++: Don't count every conversion as a use.
1 parent ecbcee5 commit f5ebcd2

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private module IteratorIndirections {
208208

209209
override predicate isAdditionalDereference(Instruction deref, Operand address) {
210210
exists(CallInstruction call |
211-
operandForfullyConvertedCall(deref.getAUse(), call) and
211+
deref = call and
212212
this = call.getStaticCallTarget().getClassAndName("operator*") and
213213
address = call.getThisArgumentOperand()
214214
)
@@ -585,6 +585,15 @@ private module Cached {
585585
)
586586
}
587587

588+
/** Holds if `op` is the unique use of a conversion-like instruction. */
589+
private predicate isConverted(Operand op, boolean isPointerArith) {
590+
exists(Instruction def |
591+
def = op.getDef() and
592+
conversionFlow(_, op.getDef(), isPointerArith) and
593+
exists(unique( | | getAUse(def)))
594+
)
595+
}
596+
588597
/**
589598
* Holds if `op` is a use of an SSA variable rooted at `base` with `ind` number
590599
* of indirections.
@@ -602,6 +611,9 @@ private module Cached {
602611
type = getLanguageType(op) and
603612
upper = countIndirectionsForCppType(type) and
604613
isUseImpl(op, base, ind0) and
614+
// Don't count every conversion as their own use. Instead, only the first
615+
// use (i.e., before any conversions are applied) will count as a use.
616+
not isConverted(op, false) and
605617
ind = ind0 + [0 .. upper] and
606618
indirectionIndex = ind - ind0
607619
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ void test_set_through_const_pointer(int *e)
533533
}
534534

535535
void sink_then_source(int* p) {
536-
sink(*p);
537-
*p = source(); // $ SPURIOUS: ir=537:10 ir=541:9
536+
sink(*p); // $ SPURIOUS: ir
537+
*p = source();
538538
}
539539

540540
void test_sink_then_source() {

0 commit comments

Comments
 (0)