Skip to content

Commit c904a33

Browse files
committed
CPP: Add test cases.
1 parent 06dd5f3 commit c904a33

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
| test.cpp:30:25:30:35 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is int *. |
55
| test.cpp:38:30:38:40 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is int *. |
66
| test.cpp:61:27:61:37 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is int *. |
7+
| test.cpp:88:35:88:47 | sizeof(MyABC) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is myChar *const. |
8+
| test.cpp:89:40:89:52 | sizeof(MyABC) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is myInt *const. |

cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,32 @@ void test7(int i) {
6464
v = *(int *)(voidPointer + i); // GOOD (actually rather dubious, but this could be correct code)
6565
v = *(int *)(voidPointer + (i * sizeof(int))); // GOOD
6666
}
67+
68+
typedef unsigned long size_t;
69+
70+
void *malloc(size_t size);
71+
72+
class MyABC
73+
{
74+
public:
75+
int a, b, c;
76+
};
77+
78+
typedef unsigned char myChar;
79+
typedef unsigned int myInt;
80+
81+
class MyTest8Class
82+
{
83+
public:
84+
MyTest8Class() :
85+
pairPtr((myChar *)malloc(sizeof(MyABC) * 2)),
86+
pairPtrInt((myInt *)malloc(sizeof(MyABC) * 2))
87+
{
88+
myChar *secondPtr = pairPtr + sizeof(MyABC); // GOOD [FALSE POSITIVE]
89+
myInt *secondPtrInt = pairPtrInt + sizeof(MyABC); // BAD
90+
}
91+
92+
private:
93+
myChar * const pairPtr;
94+
myInt * const pairPtrInt;
95+
};

0 commit comments

Comments
 (0)