mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Merge pull request #16405 from geoffw0/qhelp3
C++: Improve qhelp for StrncpyFlippedArgs.
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
strncpy(dest, src, sizeof(src)); //wrong: size of dest should be used
|
||||
strncpy(dest, src, strlen(src)); //wrong: size of dest should be used
|
||||
@@ -3,7 +3,7 @@
|
||||
"qhelp.dtd">
|
||||
<qhelp>
|
||||
<overview>
|
||||
<p>The standard library function <code>strncpy</code> copies a source string to a destination buffer. The third argument defines the maximum number of characters to copy and should be less than
|
||||
<p>The standard library function <code>strncpy</code> copies a source string to a destination buffer. The third argument defines the maximum number of characters to copy and should be less than
|
||||
or equal to the size of the destination buffer. Calls of the form <code>strncpy(dest, src, strlen(src))</code> or <code>strncpy(dest, src, sizeof(src))</code> incorrectly set the third argument to the size of the source buffer. Executing a call of this type may cause a buffer overflow. Buffer overflows can lead to anything from a segmentation fault to a security vulnerability.</p>
|
||||
|
||||
</overview>
|
||||
@@ -12,14 +12,20 @@ or equal to the size of the destination buffer. Calls of the form <code>strncpy(
|
||||
not the source buffer.</p>
|
||||
|
||||
</recommendation>
|
||||
<example><sample src="StrncpyFlippedArgs.cpp" />
|
||||
|
||||
<example>
|
||||
<p>In the following examples, the size of the source buffer is incorrectly used as a parameter to <code>strncpy</code>:</p>
|
||||
|
||||
<sample src="StrncpyFlippedArgsBad.cpp" />
|
||||
|
||||
<p>The corrected version uses the size of the destination buffer, or a variable containing the size of the destination buffer as the size parameter to <code>strncpy</code>:</p>
|
||||
|
||||
<sample src="StrncpyFlippedArgsGood.cpp" />
|
||||
</example>
|
||||
|
||||
<references>
|
||||
|
||||
<li>cplusplus.com: <a href="http://www.cplusplus.com/reference/clibrary/cstring/strncpy/">strncpy</a>.</li>
|
||||
<li>cplusplus.com: <a href="https://cplusplus.com/reference/cstring/strncpy/">strncpy</a>.</li>
|
||||
<li>
|
||||
I. Gerg. <em>An Overview and Example of the Buffer-Overflow Exploit</em>. IANewsletter vol 7 no 4. 2005.
|
||||
</li>
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
char src[256];
|
||||
char dest1[128];
|
||||
|
||||
...
|
||||
|
||||
strncpy(dest1, src, sizeof(src)); // wrong: size of dest should be used
|
||||
|
||||
char *dest2 = (char *)malloc(sz1 + sz2 + sz3);
|
||||
strncpy(dest2, src, strlen(src)); // wrong: size of dest should be used
|
||||
@@ -0,0 +1,10 @@
|
||||
char src[256];
|
||||
char dest1[128];
|
||||
|
||||
...
|
||||
|
||||
strncpy(dest1, src, sizeof(dest1)); // correct
|
||||
|
||||
size_t destSize = sz1 + sz2 + sz3;
|
||||
char *dest2 = (char *)malloc(destSize);
|
||||
strncpy(dest2, src, destSize); // correct
|
||||
Reference in New Issue
Block a user