Skip to content

Commit 7b8d164

Browse files
committed
C++: Add more good test cases.
1 parent 62c432f commit 7b8d164

File tree

1 file changed

+26
-1
lines changed
  • cpp/ql/test/query-tests/Security/CWE/CWE-416/semmle/tests/UseOfStringAfterLifetimeEnds

1 file changed

+26
-1
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-416/semmle/tests/UseOfStringAfterLifetimeEnds/test.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void call_by_cref(const S&);
161161

162162
void call(const char*);
163163

164-
const char* test(bool b1, bool b2) {
164+
const char* test1(bool b1, bool b2) {
165165
auto s1 = std::string("hello").c_str(); // BAD
166166
auto s2 = b1 ? std::string("hello").c_str() : ""; // BAD
167167
auto s3 = b2 ? "" : std::string("hello").c_str(); // BAD
@@ -186,3 +186,28 @@ const char* test(bool b1, bool b2) {
186186

187187
return std::string("hello").c_str(); // BAD
188188
}
189+
190+
void test2(bool b1, bool b2) {
191+
std::string s("hello");
192+
auto s1 = s.c_str(); // GOOD
193+
auto s2 = b1 ? s.c_str() : ""; // GOOD
194+
auto s3 = b2 ? "" : s.c_str(); // GOOD
195+
const char* s4;
196+
s4 = s.c_str(); // GOOD
197+
198+
std::string& sRef = s;
199+
200+
auto s5 = sRef.c_str(); // GOOD
201+
auto s6 = b1 ? sRef.c_str() : ""; // GOOD
202+
auto s7 = b2 ? "" : sRef.c_str(); // GOOD
203+
const char* s8;
204+
s8 = sRef.c_str(); // GOOD
205+
206+
std::string&& sRefRef = std::string("hello");
207+
208+
auto s9 = sRefRef.c_str(); // GOOD
209+
auto s10 = b1 ? sRefRef.c_str() : ""; // GOOD
210+
auto s11 = b2 ? "" : sRefRef.c_str(); // GOOD
211+
const char* s12;
212+
s12 = sRefRef.c_str(); // GOOD
213+
}

0 commit comments

Comments
 (0)