-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: Add an experimental query for surprising lifetimes from range-based for loops #15939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++: Add an experimental query for surprising lifetimes from range-based for loops #15939
Conversation
NOTE: This is with the yet-to-be-merged changes to the extractor and IR generation.
0cf1838
to
e23e3d7
Compare
QHelp previews: cpp/ql/src/experimental/Security/CWE/CWE-416/IteratorToExpiredContainer.qhelpIterator to expired containerUsing 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. RecommendationNever create an iterator to a temporary container when the iterator is expected to be used after the container's lifetime has expired. ExampleThe rules for lifetime extension ensures that the code in #include <vector>
std::vector<int> get_vector();
void use(int);
void lifetime_of_temp_extended() {
for(auto x : get_vector()) {
use(x); // GOOD: The lifetime of the vector returned by `get_vector()` is extended until the end of the loop.
}
}
// Writes the the values of `v` to an external log and returns it unchanged.
const std::vector<int>& log_and_return_argument(const std::vector<int>& v);
void lifetime_of_temp_not_extended() {
for(auto x : log_and_return_argument(get_vector())) {
use(x); // BAD: The lifetime of the vector returned by `get_vector()` is not extended, and the behavior is undefined.
}
}
References
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a great query, and the .qhelp
is a good explanation of the problem (with references for more detail). 👍
Some questions below.
We also need to do a DCA run, docs review, and a closer examination of real world results ... but for each of these, I don't really mind whether we do it now or when we're ready to promote the query out of experimental. It would certainly help to have the extractor change merged.
cpp/ql/test/experimental/query-tests/Security/CWE/CWE-416/test.cpp
Outdated
Show resolved
Hide resolved
cpp/ql/test/experimental/query-tests/Security/CWE/CWE-416/test.cpp
Outdated
Show resolved
Hide resolved
cpp/ql/src/experimental/Security/CWE/CWE-416/IteratorToExpiredContainer.ql
Outdated
Show resolved
Hide resolved
cpp/ql/src/experimental/Security/CWE/CWE-416/IteratorToExpiredContainer.ql
Outdated
Show resolved
Hide resolved
cpp/ql/src/experimental/Security/CWE/CWE-416/IteratorToExpiredContainer.ql
Show resolved
Hide resolved
Partial answer: See what I wrote above:
Basically, there will be no real world results before the extractor (and analysis) changes are merged. Even then, we have to wait for MRVA databases to update before we can even run this on MRVA. |
…Container.ql Co-authored-by: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
…Container.ql Co-authored-by: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 from Docs
I believe I've addressed all your comments now @geoffw0. In terms of
I believe it's best merge this query, and then we can do proper MRVA result investigations and DCA tests once we move the query out of experimental (since, at that point, we'll have both the QL and extractor changes merged, and possibly even MRVA data depending on how lucky we are with the timing of a release 🤞) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and 👍 from me.
This PR adds an experimental query to detect issues such as:
I think the precision of this query is actually perfectly suited for Code Scanning (🤞), but since the query depends on ongoing work on both the extractor side and the analysis side it'll start its lifetime (pun very much intended!) in experimental.
All the
NOT DETECTED
tests are actually detected when running with the yet-to-be-released version of the extractor and with #15858 merged.I don't think it makes much sense to run this on DCA yet as that DCA run will mostly test #15858 (and the internal extractor changes) I guess. I'll try doing it anyway just for fun, though!