mirror of
https://github.com/github/codeql.git
synced 2026-04-26 09:15:12 +02:00
C++: Add a 'good' example as well.
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
Record *mkRecord(int value) {
|
||||
Record myRecord(value);
|
||||
|
||||
return &myRecord; // BAD: return a pointer to `myRecord`, which is a stack-allocated object
|
||||
}
|
||||
@@ -5,22 +5,23 @@
|
||||
|
||||
|
||||
<overview>
|
||||
<p>This rule finds return statements that return pointers to an object allocated on the stack.
|
||||
The lifetime of a stack allocated memory location only lasts until the function returns, and
|
||||
the contents of that memory become undefined after that. Clearly, using a pointer to stack
|
||||
<p>This rule finds return statements that return pointers to an object allocated on the stack.
|
||||
The lifetime of a stack allocated memory location only lasts until the function returns, and
|
||||
the contents of that memory become undefined after that. Clearly, using a pointer to stack
|
||||
memory after the function has already returned will have undefined results. </p>
|
||||
|
||||
</overview>
|
||||
<recommendation>
|
||||
<p>Use the functions of the <tt>malloc</tt> family to dynamically allocate memory on the heap for data that is used across function calls.</p>
|
||||
<p>Use the functions of the <tt>malloc</tt> family, or <tt>new</tt>, to dynamically allocate memory on the heap for data that is used across function calls.</p>
|
||||
|
||||
</recommendation>
|
||||
<example><sample src="ReturnStackAllocatedMemory.cpp" />
|
||||
|
||||
|
||||
|
||||
|
||||
<example>
|
||||
<p>The following example allocates an object on the stack and returns a pointer to it. This is incorrect because the object is deallocated
|
||||
when the function returns, and the pointer becomes invalid.</p>
|
||||
<sample src="ReturnStackAllocatedMemoryBad.cpp" />
|
||||
|
||||
<p>To fix this, allocate the object on the heap using <tt>new</tt> and return a pointer to the heap-allocated object.</p>
|
||||
<sample src="ReturnStackAllocatedMemoryGood.cpp" />
|
||||
</example>
|
||||
|
||||
<references>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
Record *mkRecord(int value) {
|
||||
Record myRecord(value);
|
||||
|
||||
return &myRecord; // BAD: returns a pointer to `myRecord`, which is a stack-allocated object.
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
Record *mkRecord(int value) {
|
||||
Record *myRecord = new Record(value);
|
||||
|
||||
return myRecord; // GOOD: returns a pointer to a `myRecord`, which is a heap-allocated object.
|
||||
}
|
||||
Reference in New Issue
Block a user