Skip to content

Commit 4630c69

Browse files
committed
C++: Add a test case resembling the example from ODASA-3940.
1 parent 857a4d8 commit 4630c69

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| test.c:17:20:17:25 | call to malloc | Type 'double' is 8 bytes, but only 5 bytes are allocated. |
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. |
5+
| test.c:59:15:59:20 | call to malloc | Type 'MyUnion' is 128 bytes, but only 8 bytes are allocated. |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
| test.c:17:20:17:25 | call to malloc | Allocated memory (5 bytes) is not a multiple of the size of 'double' (8 bytes). |
77
| test.c:32:19:32:24 | call to malloc | Allocated memory (2 bytes) is not a multiple of the size of 'float' (4 bytes). |
88
| test.c:33:20:33:25 | call to malloc | Allocated memory (4 bytes) is not a multiple of the size of 'double' (8 bytes). |
9+
| test.c:59:15:59:20 | call to malloc | Allocated memory (8 bytes) is not a multiple of the size of 'MyUnion' (128 bytes). |

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,18 @@ void good1(void) {
4343
free(dptr);
4444
}
4545

46-
47-
46+
typedef struct _myStruct
47+
{
48+
int x, y;
49+
} MyStruct;
50+
51+
typedef union _myUnion
52+
{
53+
MyStruct ms;
54+
char data[128];
55+
} MyUnion;
56+
57+
void test_union() {
58+
MyUnion *a = malloc(sizeof(MyUnion)); // GOOD
59+
MyUnion *b = malloc(sizeof(MyStruct)); // BAD (too small)
60+
}

0 commit comments

Comments
 (0)