Skip to content

Commit 2b9c96d

Browse files
committed
C++: Add testcase.
1 parent decd576 commit 2b9c96d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
| file://:0:0:0:0 | pointer to ~vector output argument | This object is destroyed before $@ is called. | test.cpp:780:41:780:45 | call to begin | call to begin |
2+
| file://:0:0:0:0 | pointer to ~vector output argument | This object is destroyed before $@ is called. | test.cpp:780:56:780:58 | call to end | call to end |
13
| test.cpp:680:30:680:30 | call to operator[] | This object is destroyed before $@ is called. | test.cpp:680:17:680:17 | call to begin | call to begin |
24
| test.cpp:680:30:680:30 | call to operator[] | This object is destroyed before $@ is called. | test.cpp:680:17:680:17 | call to end | call to end |
35
| test.cpp:683:31:683:32 | call to at | This object is destroyed before $@ is called. | test.cpp:683:17:683:17 | call to begin | call to begin |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-416/test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,4 +770,26 @@ void test2() {
770770
void test3() {
771771
const std::vector<std::vector<int>>& v = returnValue(); // GOOD
772772
for(const std::vector<int>& x : v) {}
773+
}
774+
775+
struct A : public std::vector<int> {
776+
void foo(std::vector<int>& result) {
777+
int i = 0;
778+
while (i < 10) {
779+
A chunk;
780+
result.insert(result.end(), chunk.begin(), chunk.end());
781+
++i;
782+
}
783+
}
784+
785+
~A() = default;
786+
};
787+
788+
void test4() {
789+
// This creates a temporary, after which `~A` is called at the semicolon, and
790+
// `~A` calls `~vector<int>` inside the compiler-generated destructor.
791+
// If we don't preserve the call context and return to the destructor call in this
792+
// function we may end up in the destructor call `chunk.~A()`in `A.foo`. This destructor
793+
// call can flow to `begin` through the back-edge and cause a strange FP.
794+
auto zero = A().size();
773795
}

0 commit comments

Comments
 (0)