-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: Add a new query for calling c_str
on temporary objects
#14928
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
Conversation
d4ed5d6
to
78671a9
Compare
78671a9
to
9c59094
Compare
9c59094
to
e10caa6
Compare
QHelp previews: cpp/ql/src/Security/CWE/CWE-416/UseOfStringAfterLifetimeEnds.qhelpUse of string after lifetime endsCalling RecommendationEnsure that the pointer returned by ExampleThe following example concatenates two #include <string>
void work(const char*);
// BAD: the concatenated string is deallocated when `c_str` returns. So `work`
// is given a pointer to invalid memory.
void work_with_combined_string_bad(std::string s1, std::string s2) {
const char* combined_string = (s1 + s2).c_str();
work(combined_string);
} The following example fixes the above code by ensuring that the pointer returned by the call to #include <string>
void work(const char*);
// GOOD: the concatenated string outlives the call to `work`. So the pointer
// obtainted from `c_str` is valid.
void work_with_combined_string_good(std::string s1, std::string s2) {
auto combined_string = s1 + s2;
work(combined_string.c_str());
} 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.
Two small comments for now.
cpp/ql/test/query-tests/Security/CWE/CWE-416/semmle/tests/UseOfStringAfterLifetimeEnds/test.cpp
Outdated
Show resolved
Hide resolved
cpp/ql/test/query-tests/Security/CWE/CWE-416/semmle/tests/UseOfStringAfterLifetimeEnds/test.cpp
Outdated
Show resolved
Hide resolved
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.
A couple of comments below.
Query, library changes, qhelp, test otherwise LGTM.
I'm looking at some real world results as well and will comment on those later.
cpp/ql/src/Security/CWE/CWE-416/UseOfStringAfterLifetimeEnds.ql
Outdated
Show resolved
Hide resolved
Real world results: I did a smaller MRVA run and tested on some local databases as well. Found two results, both LGTM. 👍 |
I'm happy with this PR and I think it's a great new query. Looks like @jketema is happy too. DCA LGTM. I think we just need the docs team to take a look (I've taken the liberty of adding the |
I haven't looked at all the details, but I trust @geoffw0's judgement here. |
Thank you. I was indeed going to do that as soon as I had gotten a 👍 from the team. |
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.
Thanks for the review request. This looks good. Just a couple of small comments.
cpp/ql/src/Security/CWE/CWE-416/UseOfStringAfterLifetimeEnds.qhelp
Outdated
Show resolved
Hide resolved
cpp/ql/src/Security/CWE/CWE-416/UseOfStringAfterLifetimeEnds.qhelp
Outdated
Show resolved
Hide resolved
Co-authored-by: Felicity Chapman <felicitymay@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.
Thanks for the text updates. All LGTM.
Coding Standards failure is unrelated. Merging! 🤠 |
This PR adds a new query that looks for variations of:
MRVA results look really good. On the top 1000 projects I'm seeing 25 results, and they all look like TPs 🎉.
In the future I'd like to extend the query to support more lifetimes than just "temporary objects", but for now this is good enough, I think.