From 708d12624f05ad8891864d499c64ff2ee3039e43 Mon Sep 17 00:00:00 2001
From: Mathias Vorreiter Pedersen
Date: Tue, 30 Apr 2024 16:32:32 +0100
Subject: [PATCH] C++: Update documentation on
'cpp/iterator-to-expired-container'.
---
.../Security/CWE/CWE-416/IteratorToExpiredContainer.qhelp | 6 ++++++
.../IteratorToExpiredContainerExtendedLifetime-fixed.cpp | 6 ++++++
2 files changed, 12 insertions(+)
create mode 100644 cpp/ql/src/Security/CWE/CWE-416/IteratorToExpiredContainerExtendedLifetime-fixed.cpp
diff --git a/cpp/ql/src/Security/CWE/CWE-416/IteratorToExpiredContainer.qhelp b/cpp/ql/src/Security/CWE/CWE-416/IteratorToExpiredContainer.qhelp
index 19975b17493..176ead87de4 100644
--- a/cpp/ql/src/Security/CWE/CWE-416/IteratorToExpiredContainer.qhelp
+++ b/cpp/ql/src/Security/CWE/CWE-416/IteratorToExpiredContainer.qhelp
@@ -30,6 +30,12 @@ This is because the temporary container is not bound to a rvalue reference.
+
+To fix lifetime_of_temp_not_extended consider rewriting the code so that the temporary's lifetime is extended.
+In fixed_lifetime_of_temp_not_extended the lifetime of the temporary object has been extended by storing it in an rvalue reference.
+
+
+
diff --git a/cpp/ql/src/Security/CWE/CWE-416/IteratorToExpiredContainerExtendedLifetime-fixed.cpp b/cpp/ql/src/Security/CWE/CWE-416/IteratorToExpiredContainerExtendedLifetime-fixed.cpp
new file mode 100644
index 00000000000..d113b4165ff
--- /dev/null
+++ b/cpp/ql/src/Security/CWE/CWE-416/IteratorToExpiredContainerExtendedLifetime-fixed.cpp
@@ -0,0 +1,6 @@
+void fixed_lifetime_of_temp_not_extended() {
+ auto&& v = get_vector();
+ for(auto x : log_and_return_argument(v)) {
+ use(x); // GOOD: The lifetime of the container returned by `get_vector()` has been extended to the lifetime of `v`.
+ }
+}