Click to see the query in the CodeQL repository
Using an iterator owned by a container after the lifetime of the container has expired can lead to undefined behavior. This is because the iterator may be invalidated when the container is destroyed, and dereferencing an invalidated iterator is undefined behavior. These problems can be hard to spot due to C++’s complex rules for temporary object lifetimes and their extensions.
Never create an iterator to a temporary container when the iterator is expected to be used after the container’s lifetime has expired.
The rules for lifetime extension ensures that the code in lifetime_of_temp_extended is well-defined. This is because the lifetime of the temporary container returned by get_vector is extended to the end of the loop. However, prior to C++23, the lifetime extension rules do not ensure that the container returned by get_vector is extended in lifetime_of_temp_not_extended. 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 lifetime of the temporary object 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.
CERT C Coding Standard: MEM30-C. Do not access freed memory.
OWASP: Using freed memory.
Common Weakness Enumeration: CWE-416.
Common Weakness Enumeration: CWE-664.