Skip to content

Commit 7fc31f2

Browse files
committed
CPP: Basic fix.
1 parent 34d307e commit 7fc31f2

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

cpp/ql/src/Security/CWE/CWE-131/NoSpaceForZeroTerminator.ql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* external/cwe/cwe-122
1515
*/
1616
import cpp
17+
import semmle.code.cpp.dataflow.DataFlow
1718

1819
class MallocCall extends FunctionCall
1920
{
@@ -34,6 +35,12 @@ class MallocCall extends FunctionCall
3435

3536
predicate terminationProblem(MallocCall malloc, string msg) {
3637
malloc.getAllocatedSize() instanceof StrlenCall and
38+
not exists(DataFlow::Node def, DataFlow::Node use, FunctionCall fc |
39+
DataFlow::localFlow(def, use) and
40+
def.asExpr() = malloc and
41+
use.asExpr() = fc.getArgument(0) and
42+
fc.getTarget().hasName("memcpy")
43+
) and
3744
msg = "This allocation does not include space to null-terminate the string."
3845
}
3946

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
| test.c:15:20:15:25 | call to malloc | This allocation does not include space to null-terminate the string. |
22
| test.c:29:20:29:25 | call to malloc | This allocation does not include space to null-terminate the string. |
33
| test.c:44:20:44:25 | call to malloc | This allocation does not include space to null-terminate the string. |
4-
| test.c:72:17:72:22 | call to malloc | This allocation does not include space to null-terminate the string. |
54
| test.cpp:18:35:18:40 | call to malloc | This allocation does not include space to null-terminate the string. |

cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void good3(char *str) {
6767
void *memcpy(void *s1, const void *s2, size_t n);
6868

6969
void good4(char *str) {
70-
// GOOD -- allocating a non zero-terminated string [FALSE POSITIVE]
70+
// GOOD -- allocating a non zero-terminated string
7171
int len = strlen(str);
7272
char *buffer = malloc(len);
7373

0 commit comments

Comments
 (0)