Skip to content

Commit af51a0a

Browse files
committed
C++: Respond to review comments.
1 parent 81d20be commit af51a0a

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-416/UseAfterExpiredLifetime.qhelp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ Use RAII ("Resource Acquisition Is Initialization") to manage the lifetime of ob
2020
</recommendation>
2121
<example>
2222
<p>
23-
The following two functions demonstrate common lifetime violations when working with the C++ standard library.
23+
The following two examples demonstrate common lifetime violations when working with the C++ standard library.
24+
</p>
2425

26+
<p>
2527
The <code>bad_call_c_api</code> function contains a use of an expired lifetime.
2628
First, a temporary object of type <code>std::string</code> is constructed, and a pointer to its internal buffer is stored in a local variable.
2729
Once the <code>c_str()</code> call returns, the temporary object is destroyed, and the memory pointed to by <code>p</code> is freed.
@@ -31,6 +33,7 @@ The <code>good_call_c_api</code> function contains a fixed version of the first
3133
The variable <code>hello</code> is declared as a local variable, and the pointer to its internal buffer is stored in <code>p</code>.
3234
The lifetime of hello outlives the call to <code>c_api</code>, so the pointer stored in <code>p</code> remains valid throughout the call to <code>c_api</code>.
3335
</p>
36+
<sample src="UseAfterExpiredLifetime_c_api_call.cpp" />
3437

3538
<p>
3639
The <code>bad_remove_even_numbers</code> function demonstrates a potential issue with iterator invalidation.
@@ -39,9 +42,8 @@ For example, calling <code>erase</code> on an object of type <code>std::vector&l
3942

4043
The <code>good_remove_even_numbers</code> function contains a fixd version of the third example.
4144
The <code>erase</code> function returns an iterator to the element following the last element removed, and this return value is used to ensure that <code>it</code> remains valid after the call to <code>erase</code>.
42-
4345
</p>
44-
<sample src="UseAfterExpiredLifetime.cpp" />
46+
<sample src="UseAfterExpiredLifetime_iterator_invalidation.cpp" />
4547

4648
</example>
4749
<references>
@@ -58,6 +60,9 @@ OWASP:
5860
<li>
5961
<a href="https://en.cppreference.com/w/cpp/container">Containers library</a>
6062
</li>
63+
<li>
64+
<a href="https://en.cppreference.com/w/cpp/language/raii">RAII</a>
65+
</li>
6166

6267
</references>
6368
</qhelp>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
void c_api(const char*);
2+
3+
void bad_call_c_api() {
4+
// BAD: the memory returned by `c_str()` is freed when the temporary string is destroyed
5+
const char* p = std::string("hello").c_str();
6+
c_api(p);
7+
}
8+
9+
void good_call_c_api() {
10+
// GOOD: the "hello" string outlives the pointer returned by `c_str()`, so it's safe to pass it to `c_api()`
11+
std::string hello("hello");
12+
const char* p = hello.c_str();
13+
c_api(p);
14+
}

cpp/ql/src/experimental/Security/CWE/CWE-416/UseAfterExpiredLifetime.cpp renamed to cpp/ql/src/experimental/Security/CWE/CWE-416/UseAfterExpiredLifetime_iterator_invalidation.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
void c_api(const char*);
2-
3-
void bad_call_c_api() {
4-
// BAD: the memory returned by `c_str()` is freed when the temporary string is destroyed
5-
const char* p = std::string("hello").c_str();
6-
c_api(p);
7-
}
8-
9-
void good_call_c_api() {
10-
// GOOD: the "hello" string outlives the pointer returned by `c_str()`, so it's safe to pass it to `c_api()`
11-
std::string hello("hello");
12-
const char* p = hello.c_str();
13-
c_api(p);
14-
}
15-
161
void bad_remove_even_numbers(std::vector<int>& v) {
172
// BAD: the iterator is invalidated after the call to `erase`.
183
for(std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {

0 commit comments

Comments
 (0)