Skip to content

Commit 2f87873

Browse files
authored
Merge pull request #1535 from geoffw0/nospacezero
CPP: Fix false positives from NoSpaceForZeroTerminator.ql
2 parents 8c733fd + 1fd08f4 commit 2f87873

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

change-notes/1.22/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
| **Query** | **Expected impact** | **Change** |
1313
|----------------------------|------------------------|------------------------------------------------------------------|
1414
| Expression has no effect (`cpp/useless-expression`) | Fewer false positive results | Calls to functions with the `weak` attribute are no longer considered to be side effect free, because they could be overridden with a different implementation at link time. |
15+
| No space for zero terminator (`cpp/no-space-for-terminator`) | Fewer false positive results | False positives involving strings that are not null-terminated have been excluded. |
1516
| Suspicious pointer scaling (`cpp/suspicious-pointer-scaling`) | Lower precision | The precision of this query has been reduced to "medium". This coding pattern is used intentionally and safely in a number of real-world projects. Results are no longer displayed on LGTM unless you choose to display them. |
1617
| Non-constant format string (`cpp/non-constant-format`) | Fewer false positive results | Rewritten using the taint-tracking library. |
1718

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* external/cwe/cwe-122
1515
*/
1616
import cpp
17+
import semmle.code.cpp.dataflow.DataFlow
18+
import semmle.code.cpp.models.implementations.Memcpy
1719

1820
class MallocCall extends FunctionCall
1921
{
@@ -34,6 +36,13 @@ class MallocCall extends FunctionCall
3436

3537
predicate terminationProblem(MallocCall malloc, string msg) {
3638
malloc.getAllocatedSize() instanceof StrlenCall and
39+
not exists(DataFlow::Node def, DataFlow::Node use, FunctionCall fc, MemcpyFunction memcpy, int ix |
40+
DataFlow::localFlow(def, use) and
41+
def.asExpr() = malloc and
42+
fc.getTarget() = memcpy and
43+
memcpy.hasArrayOutput(ix) and
44+
use.asExpr() = fc.getArgument(ix)
45+
) and
3746
msg = "This allocation does not include space to null-terminate the string."
3847
}
3948

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ void good3(char *str) {
6363
char *buffer = malloc((strlen(str) + 1) * sizeof(char));
6464
free(buffer);
6565
}
66+
67+
void *memcpy(void *s1, const void *s2, size_t n);
68+
69+
void good4(char *str) {
70+
// GOOD -- allocating a non zero-terminated string
71+
int len = strlen(str);
72+
char *buffer = malloc(len);
73+
74+
memcpy(buffer, str, len);
75+
76+
free(buffer);
77+
}

0 commit comments

Comments
 (0)