Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3895a7e

Browse files
committed
CPP: Queries: Improve NoSpaceForZeroTerminator query.
1 parent 3c9432d commit 3895a7e

4 files changed

Lines changed: 9 additions & 11 deletions

File tree

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@
1717
import cpp
1818
import semmle.code.cpp.dataflow.DataFlow
1919
import semmle.code.cpp.models.interfaces.ArrayFunction
20+
import semmle.code.cpp.models.interfaces.Allocation
2021

21-
class MallocCall extends FunctionCall {
22-
MallocCall() { this.getTarget().hasGlobalOrStdName("malloc") }
23-
24-
Expr getAllocatedSize() { result = this.getArgument(0) }
25-
}
26-
27-
predicate terminationProblem(MallocCall malloc, string msg) {
22+
predicate terminationProblem(AllocationExpr malloc, string msg) {
2823
// malloc(strlen(...))
29-
exists(StrlenCall strlen | DataFlow::localExprFlow(strlen, malloc.getAllocatedSize())) and
24+
exists(StrlenCall strlen | DataFlow::localExprFlow(strlen, malloc.getSizeExpr())) and
3025
// flows into a null-terminated string function
3126
exists(ArrayFunction af, FunctionCall fc, int arg |
3227
DataFlow::localExprFlow(malloc, fc.getArgument(arg)) and
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
| test2.cpp:64:34:64:39 | call to calloc | This allocation does not include space to null-terminate the string. |
2+
| test2.cpp:71:28:71:34 | call to realloc | This allocation does not include space to null-terminate the string. |
13
| test.c:16:20:16:25 | call to malloc | This allocation does not include space to null-terminate the string. |
24
| test.c:32:20:32:25 | call to malloc | This allocation does not include space to null-terminate the string. |
35
| test.c:49:20:49:25 | call to malloc | This allocation does not include space to null-terminate the string. |
46
| test.cpp:24:35:24:40 | call to malloc | This allocation does not include space to null-terminate the string. |
57
| test.cpp:63:28:63:33 | call to malloc | This allocation does not include space to null-terminate the string. |
68
| test.cpp:71:28:71:33 | call to malloc | This allocation does not include space to null-terminate the string. |
9+
| test.cpp:106:24:106:48 | new[] | This allocation does not include space to null-terminate the string. |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void good2(char *str, char *dest) {
102102
}
103103

104104
void bad9(wchar_t *wstr) {
105-
// BAD -- using new [NOT DETECTED]
105+
// BAD -- using new
106106
wchar_t *wbuffer = new wchar_t[wcslen(wstr)];
107107
wcscpy(wbuffer, wstr);
108108
delete wbuffer;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ void bad2(wchar_t *str) {
6060
}
6161

6262
void bad3(wchar_t *str) {
63-
// BAD -- Not allocating space for '\0' terminator [NOT DETECTED]
63+
// BAD -- Not allocating space for '\0' terminator
6464
wchar_t *buffer = (wchar_t *)calloc(sizeof(wchar_t), wcslen(str));
6565
wcscpy(buffer, str);
6666
free(buffer);
6767
}
6868

6969
void bad4(char *str) {
70-
// BAD -- Not allocating space for '\0' terminator [NOT DETECTED]
70+
// BAD -- Not allocating space for '\0' terminator
7171
char *buffer = (char *)realloc(0, strlen(str));
7272
strcpy(buffer, str);
7373
free(buffer);

0 commit comments

Comments
 (0)