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`. + } +}