C++: docs for AllocaInLoop

This commit is contained in:
Robert Marsh
2019-03-06 09:35:17 -08:00
parent bea75e2d1c
commit 0efb110512
3 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p> The <code>alloca</code> macro allocates memory by expanding the current stack frame.Invoking
<code>alloca</code> within a loop may lead to a stack overflow because the memory is not released
until the function returns.
</p>
</overview>
<recommendation>
<p>
Consider invoking <code>alloca</code> once outside the loop, or using <code>malloc</code> or
<code>new</code> to allocate memory on the heap if the allocation must be done inside the loop.
</p>
</recommendation>
<example>
<p>The variable <code>path</code> is allocated inside a loop with <code>alloca</code>. Consequently,
storage for all copies of the path is present in the stack frame until the end of the function.
</p>
<sample src="AllocaInLoopBad.cpp" />
<p>In the revised example, <code>path</code> is allocated with <code>malloc</code> and freed at the
end of the loop.
</p>
<sample src="AllocaInLoopGood.cpp" />
</example>
<references>
<li>Linux Programmer's Manual: <a href="http://man7.org/linux/man-pages/man3/alloca.3.html">ALLOCA(3)</a>.</li>
</references>
</qhelp>

View File

@@ -0,0 +1,8 @@
char *dir_path;
char **dir_entries;
int count;
for (int i = 0; i < count; i++) {
char *path = (char*)alloca(strlen(dir_path) + strlen(dir_entry[i]) + 2);
// use path
}

View File

@@ -0,0 +1,9 @@
char *dir_path;
char **dir_entries;
int count;
for (int i = 0; i < count; i++) {
char *path = (char*)malloc(strlen(dir_path) + strlen(dir_entry[i]) + 2);
// use path
free(path);
}