Skip to content

Commit 9ae503a

Browse files
authored
Add files via upload
1 parent 6966453 commit 9ae503a

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// BAD: if buffer does not have a terminal zero, then access outside the allocated memory is possible.
2+
3+
buffer[strlen(buffer)] = 0;
4+
5+
6+
// GOOD: we will eliminate dangerous behavior if we use a different method of calculating the length.
7+
size_t len;
8+
...
9+
buffer[len] = 0
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Potentially dangerous use of the strlen function to calculate the length of a string.
7+
The expression <code>buffer[strlen(buffer)] = 0</code> is potentially dangerous, if the variable buffer does not have a terminal zero, then access beyond the bounds of the allocated memory is possible, which will lead to undefined behavior.
8+
If terminal zero is present, then the specified expression is meaningless.</p>
9+
10+
<p>False positives include heavily nested strlen. This situation is unlikely.</p>
11+
12+
</overview>
13+
<recommendation>
14+
15+
<p>We recommend using another method for calculating the string length</p>
16+
17+
</recommendation>
18+
<example>
19+
<p>The following example demonstrates an erroneous and corrected use of the strlen function.</p>
20+
<sample src="AccessOfMemoryLocationAfterEndOfBuffer.c" />
21+
22+
</example>
23+
<references>
24+
25+
<li>
26+
CERT C Coding Standard:
27+
<a href="https://wiki.sei.cmu.edu/confluence/display/c/STR32-C.+Do+not+pass+a+non-null-terminated+character+sequence+to+a+library+function+that+expects+a+string">STR32-C. Do not pass a non-null-terminated character sequence to a library function that expects a string</a>.
28+
</li>
29+
30+
</references>
31+
</qhelp>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @name Access Of Memory Location After End Of Buffer
3+
* @description --The expression buffer [strlen (buffer)] = 0 is potentially dangerous, if the variable buffer does not have a terminal zero, then access beyond the bounds of the allocated memory is possible, which will lead to undefined behavior.
4+
* --If terminal zero is present, then the specified expression is meaningless.
5+
* --We recommend using another method for calculating the string length.
6+
* @kind problem
7+
* @id cpp/access-memory-location-after-end-buffer
8+
* @problem.severity warning
9+
* @precision medium
10+
* @tags correctness
11+
* security
12+
* external/cwe/cwe-788
13+
*/
14+
15+
import cpp
16+
import semmle.code.cpp.valuenumbering.HashCons
17+
18+
from FunctionCall fc, AssignExpr expr, ArrayExpr exprarr
19+
where
20+
fc.getTarget().hasGlobalOrStdName("strlen") and
21+
exprarr = expr.getLValue() and
22+
expr.getRValue().getValue().toInt() = 0 and
23+
exprarr.getArrayOffset() = fc and
24+
hashCons(fc.getArgument(0)) = hashCons(exprarr.getArrayBase())
25+
select expr, "use a different method to calculate the length."

0 commit comments

Comments
 (0)