Skip to content

Commit 99940a6

Browse files
authored
Merge pull request github#16440 from MathiasVP/fix-iterator-to-expired-container-fp-2
C++: Fix location of SSA def for local variable addresses
2 parents 85e71c3 + 53c2d2f commit 99940a6

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import semmle.code.cpp.models.interfaces.PartialFlow as PartialFlow
99
private import semmle.code.cpp.models.interfaces.FunctionInputsAndOutputs as FIO
1010
private import semmle.code.cpp.ir.internal.IRCppLanguage
1111
private import semmle.code.cpp.ir.dataflow.internal.ModelUtil
12+
private import semmle.code.cpp.ir.implementation.raw.internal.TranslatedInitialization
1213
private import DataFlowPrivate
1314
import SsaInternalsCommon
1415

@@ -329,6 +330,17 @@ private predicate sourceVariableHasBaseAndIndex(SourceVariable v, BaseSourceVari
329330
v.getIndirection() = ind
330331
}
331332

333+
/**
334+
* Gets the instruction that computes the address that's used to
335+
* initialize `v`.
336+
*/
337+
private Instruction getInitializationTargetAddress(IRVariable v) {
338+
exists(TranslatedVariableInitialization init |
339+
init.getIRVariable() = v and
340+
result = init.getTargetAddress()
341+
)
342+
}
343+
332344
/** An initial definition of an `IRVariable`'s address. */
333345
private class DefAddressImpl extends DefImpl, TDefAddressImpl {
334346
BaseIRVariable v;
@@ -347,8 +359,15 @@ private class DefAddressImpl extends DefImpl, TDefAddressImpl {
347359
final override Node0Impl getValue() { none() }
348360

349361
final override predicate hasIndexInBlock(IRBlock block, int index) {
350-
block = v.getIRVariable().getEnclosingIRFunction().getEntryBlock() and
351-
index = 0
362+
exists(IRVariable var | var = v.getIRVariable() |
363+
block.getInstruction(index) = getInitializationTargetAddress(var)
364+
or
365+
// If there is no translatated element that does initialization of the
366+
// variable we place the SSA definition at the entry block of the function.
367+
not exists(getInitializationTargetAddress(var)) and
368+
block = var.getEnclosingIRFunction().getEntryBlock() and
369+
index = 0
370+
)
352371
}
353372

354373
override Cpp::Location getLocation() { result = v.getIRVariable().getLocation() }
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
| test.cpp:680:30:680:30 | call to operator[] | This object is destroyed at the end of the full-expression. |
22
| test.cpp:683:31:683:32 | call to at | This object is destroyed at the end of the full-expression. |
3-
| test.cpp:689:46:689:58 | pointer to ~vector output argument | This object is destroyed at the end of the full-expression. |
43
| test.cpp:702:27:702:27 | call to operator[] | This object is destroyed at the end of the full-expression. |
54
| test.cpp:727:23:727:23 | call to operator[] | This object is destroyed at the end of the full-expression. |
65
| test.cpp:735:23:735:23 | call to operator[] | This object is destroyed at the end of the full-expression. |
7-
| test.cpp:803:3:803:3 | pointer to ~vector output argument | This object is destroyed at the end of the full-expression. |

cpp/ql/test/query-tests/Security/CWE/CWE-416/semmle/tests/IteratorToExpiredContainer/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ void test() {
686686
for (auto x : returnRef()[0]) {} // GOOD
687687
for (auto x : returnRef().at(0)) {} // GOOD
688688

689-
for(auto it = returnValue().begin(); it != returnValue().end(); ++it) {} // BAD
689+
for(auto it = returnValue().begin(); it != returnValue().end(); ++it) {} // BAD [NOT DETECTED]
690690

691691
{
692692
auto v = returnValue();
@@ -800,5 +800,5 @@ void test5(int i)
800800
const auto& vvs = returnValue();
801801
for(const auto& vs : vvs) { }
802802
++i;
803-
} // GOOD [FALSE POSITIVE]
803+
} // GOOD
804804
}

0 commit comments

Comments
 (0)