Skip to content

Commit 351caac

Browse files
committed
C++: Add GOOD and BAD comments to qhelp examples.
1 parent 8afd928 commit 351caac

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

cpp/ql/src/Security/CWE/CWE-416/UseOfStringAfterLifetimeEndsBad.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <string>
22
void work(const char*);
33

4+
// BAD: the concatenated string is deallocated when `c_str` returns. So `work`
5+
// is given a pointer to invalid memory.
46
void work_with_combined_string_bad(std::string s1, std::string s2) {
57
const char* combined_string = (s1 + s2).c_str();
68
work(combined_string);

cpp/ql/src/Security/CWE/CWE-416/UseOfStringAfterLifetimeEndsGood.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <string>
22
void work(const char*);
33

4+
// GOOD: the concatenated string outlives the call to `work`. So the pointer
5+
// obtainted from `c_str` is valid.
46
void work_with_combined_string_good(std::string s1, std::string s2) {
57
auto combined_string = s1 + s2;
68
work(combined_string.c_str());

0 commit comments

Comments
 (0)