Skip to content

Commit 2023abd

Browse files
committed
C++: Update the queries.
1 parent a9aa671 commit 2023abd

File tree

14 files changed

+38
-22
lines changed

14 files changed

+38
-22
lines changed

cpp/ql/src/Critical/SizeCheck.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import cpp
1717
import semmle.code.cpp.models.Models
1818

19-
predicate baseType(AllocationExpr alloc, Type base) {
19+
predicate baseType(HeuristicAllocationExpr alloc, Type base) {
2020
exists(PointerType pointer |
2121
pointer.getBaseType() = base and
2222
(
@@ -34,12 +34,12 @@ predicate decideOnSize(Type t, int size) {
3434
size = min(t.getSize())
3535
}
3636

37-
from AllocationExpr alloc, Type base, int basesize, int allocated
37+
from HeuristicAllocationExpr alloc, Type base, int basesize, int allocated
3838
where
3939
baseType(alloc, base) and
4040
allocated = alloc.getSizeBytes() and
4141
decideOnSize(base, basesize) and
42-
alloc.(FunctionCall).getTarget() instanceof AllocationFunction and // exclude `new` and similar
42+
alloc.(FunctionCall).getTarget() instanceof HeuristicAllocationFunction and // exclude `new` and similar
4343
basesize > allocated
4444
select alloc,
4545
"Type '" + base.getName() + "' is " + basesize.toString() + " bytes, but only " +

cpp/ql/src/Critical/SizeCheck2.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import cpp
1717
import semmle.code.cpp.models.Models
1818

19-
predicate baseType(AllocationExpr alloc, Type base) {
19+
predicate baseType(HeuristicAllocationExpr alloc, Type base) {
2020
exists(PointerType pointer |
2121
pointer.getBaseType() = base and
2222
(
@@ -34,12 +34,12 @@ predicate decideOnSize(Type t, int size) {
3434
size = min(t.getSize())
3535
}
3636

37-
from AllocationExpr alloc, Type base, int basesize, int allocated
37+
from HeuristicAllocationExpr alloc, Type base, int basesize, int allocated
3838
where
3939
baseType(alloc, base) and
4040
allocated = alloc.getSizeBytes() and
4141
decideOnSize(base, basesize) and
42-
alloc.(FunctionCall).getTarget() instanceof AllocationFunction and // exclude `new` and similar
42+
alloc.(FunctionCall).getTarget() instanceof HeuristicAllocationFunction and // exclude `new` and similar
4343
// If the codebase has more than one type with the same name, check if any matches
4444
not exists(int size | base.getSize() = size |
4545
size = 0 or

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import semmle.code.cpp.models.interfaces.ArrayFunction
2121
import semmle.code.cpp.models.interfaces.Allocation
2222
import semmle.code.cpp.commons.NullTermination
2323

24-
predicate terminationProblem(AllocationExpr malloc, string msg) {
24+
predicate terminationProblem(HeuristicAllocationExpr malloc, string msg) {
2525
// malloc(strlen(...))
2626
exists(StrlenCall strlen | DataFlow::localExprFlow(strlen, malloc.getSizeExpr())) and
2727
// flows to a call that implies this is a null-terminated string

cpp/ql/src/Security/CWE/CWE-190/TaintedAllocationSize.ql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ import DataFlow::PathGraph
2525
* Holds if `alloc` is an allocation, and `tainted` is a child of it that is a
2626
* taint sink.
2727
*/
28-
predicate allocSink(Expr alloc, DataFlow::Node sink) {
28+
predicate allocSink(HeuristicAllocationExpr alloc, DataFlow::Node sink) {
2929
exists(Expr e | e = sink.asConvertedExpr() |
30-
isAllocationExpr(alloc) and
3130
e = alloc.getAChild() and
3231
e.getUnspecifiedType() instanceof IntegralType
3332
)

cpp/ql/test/query-tests/Critical/OverflowCalculated/NoSpaceForZeroTerminator.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
| tests3.cpp:25:21:25:31 | call to malloc | This allocation does not include space to null-terminate the string. |
88
| tests3.cpp:30:21:30:31 | call to malloc | This allocation does not include space to null-terminate the string. |
99
| tests3.cpp:53:17:53:44 | new[] | This allocation does not include space to null-terminate the string. |
10+
| tests3.cpp:81:20:81:28 | call to MyMalloc1 | This allocation does not include space to null-terminate the string. |
11+
| tests3.cpp:84:20:84:28 | call to MyMalloc2 | This allocation does not include space to null-terminate the string. |

cpp/ql/test/query-tests/Critical/OverflowCalculated/tests3.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ void tests4()
7878
char *buffer1 = 0;
7979
char *buffer2 = 0;
8080

81-
buffer1 = (char *)MyMalloc1(strlen(str4)); // BAD [NOT DETECTED]
81+
buffer1 = (char *)MyMalloc1(strlen(str4)); // BAD
8282
strcpy(buffer1, str4);
8383

84-
buffer2 = (char *)MyMalloc2(strlen(str4)); // BAD [NOT DETECTED]
84+
buffer2 = (char *)MyMalloc2(strlen(str4)); // BAD
8585
strcpy(buffer2, str4);
8686
}

cpp/ql/test/query-tests/Critical/SizeCheck/SizeCheck.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
| test.c:32:19:32:24 | call to malloc | Type 'float' is 4 bytes, but only 2 bytes are allocated. |
44
| test.c:33:20:33:25 | call to malloc | Type 'double' is 8 bytes, but only 4 bytes are allocated. |
55
| test.c:59:15:59:20 | call to malloc | Type 'MyUnion' is 128 bytes, but only 8 bytes are allocated. |
6+
| test.c:69:20:69:28 | call to MyMalloc1 | Type 'float' is 4 bytes, but only 3 bytes are allocated. |
7+
| test.c:70:20:70:28 | call to MyMalloc2 | Type 'float' is 4 bytes, but only 3 bytes are allocated. |

cpp/ql/test/query-tests/Critical/SizeCheck/SizeCheck2.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
| test2.c:17:20:17:25 | call to malloc | Allocated memory (33 bytes) is not a multiple of the size of 'double' (8 bytes). |
33
| test2.c:32:23:32:28 | call to malloc | Allocated memory (28 bytes) is not a multiple of the size of 'long long' (8 bytes). |
44
| test2.c:33:20:33:25 | call to malloc | Allocated memory (20 bytes) is not a multiple of the size of 'double' (8 bytes). |
5+
| test2.c:53:21:53:29 | call to MyMalloc1 | Allocated memory (33 bytes) is not a multiple of the size of 'double' (8 bytes). |
6+
| test2.c:54:21:54:29 | call to MyMalloc2 | Allocated memory (33 bytes) is not a multiple of the size of 'double' (8 bytes). |

cpp/ql/test/query-tests/Critical/SizeCheck/test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ void *MyMalloc2(size_t size);
6666

6767
void customAllocatorTests()
6868
{
69-
float *fptr1 = MyMalloc1(3); // BAD (too small) [NOT DETECTED]
70-
float *fptr2 = MyMalloc2(3); // BAD (too small) [NOT DETECTED]
69+
float *fptr1 = MyMalloc1(3); // BAD (too small)
70+
float *fptr2 = MyMalloc2(3); // BAD (too small)
7171
}

cpp/ql/test/query-tests/Critical/SizeCheck/test2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ void *MyMalloc2(size_t size);
5050

5151
void customAllocatorTests()
5252
{
53-
double *dptr1 = MyMalloc1(33); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
54-
double *dptr2 = MyMalloc2(33); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
53+
double *dptr1 = MyMalloc1(33); // BAD -- Not a multiple of sizeof(double)
54+
double *dptr2 = MyMalloc2(33); // BAD -- Not a multiple of sizeof(double)
5555
}

0 commit comments

Comments
 (0)