Skip to content

Commit 4ab9b81

Browse files
committed
C++: Add tests exposing some FP's for OverflowStatic query
1 parent 1bbadb5 commit 4ab9b81

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
| test.c:15:9:15:13 | access to array | Potential buffer-overflow: 'xs' has size 5 but 'xs[6]' is accessed here. |
1010
| test.c:20:9:20:18 | access to array | Potential buffer-overflow: 'ys' has size 5 but 'ys[5]' is accessed here. |
1111
| test.c:21:9:21:18 | access to array | Potential buffer-overflow: 'ys' has size 5 but 'ys[6]' is accessed here. |
12+
| test.c:39:3:39:11 | access to array | Potential buffer-overflow: 'buf' has size 1 but 'buf[7]' is accessed here. |
13+
| test.c:40:3:40:11 | access to array | Potential buffer-overflow: 'buf' has size 1 but 'buf[8]' is accessed here. |
14+
| test.c:51:3:51:20 | access to array | Potential buffer-overflow: 'ptr' has size 1 but 'ptr[7]' is accessed here. |
15+
| test.c:52:3:52:18 | access to array | Potential buffer-overflow: 'ptr' has size 1 but 'ptr[8]' is accessed here. |
16+
| test.c:58:3:58:28 | access to array | Potential buffer-overflow: 'ptr' has size 1 but 'ptr[7]' is accessed here. |
17+
| test.c:59:3:59:26 | access to array | Potential buffer-overflow: 'ptr' has size 1 but 'ptr[8]' is accessed here. |
18+
| test.c:65:3:65:20 | access to array | Potential buffer-overflow: 'ptr' has size 1 but 'ptr[7]' is accessed here. |
19+
| test.c:66:3:66:18 | access to array | Potential buffer-overflow: 'ptr' has size 1 but 'ptr[8]' is accessed here. |
20+
| test.c:72:3:72:11 | access to array | Potential buffer-overflow: 'buf' has size 1 but 'buf[1]' is accessed here. |
1221
| test.cpp:19:3:19:12 | access to array | Potential buffer-overflow: counter 'i' <= 3 but 'buffer1' has 3 elements. |
1322
| test.cpp:20:3:20:12 | access to array | Potential buffer-overflow: counter 'i' <= 3 but 'buffer2' has 3 elements. |
1423
| test.cpp:24:27:24:27 | 4 | Potential buffer-overflow: 'buffer1' has size 3 not 4. |

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,47 @@ void f(void) {
2727
c = stru.zs[6]; // GOOD (zs is variable size)
2828
}
2929

30+
void* malloc(long unsigned int);
31+
typedef struct {
32+
char len;
33+
char buf[1];
34+
} var_buf;
35+
36+
void test_buffer_sentinal() {
37+
var_buf *b = malloc(10); // len(buf.buffer) effectively 8
38+
b->buf[0] = 0; // GOOD
39+
b->buf[7] = 0; // GOOD [FALSE POSITIVE]
40+
b->buf[8] = 0; // BAD
41+
}
42+
43+
union u {
44+
unsigned long value;
45+
char ptr[1];
46+
};
47+
48+
void union_test() {
49+
union u u;
50+
u.ptr[0] = 0; // GOOD
51+
u.ptr[sizeof(u)-1] = 0; // GOOD [FALSE POSITIVE]
52+
u.ptr[sizeof(u)] = 0; // BAD
53+
}
54+
55+
void test_struct_union() {
56+
struct { union u u; } v;
57+
v.u.ptr[0] = 0; // GOOD
58+
v.u.ptr[sizeof(union u)-1] = 0; // GOOD [FALSE POSITIVE]
59+
v.u.ptr[sizeof(union u)] = 0; // BAD
60+
}
61+
62+
void union_test2() {
63+
union { char ptr[1]; unsigned long value; } u;
64+
u.ptr[0] = 0; // GOOD
65+
u.ptr[sizeof(u)-1] = 0; // GOOD [FALSE POSITIVE]
66+
u.ptr[sizeof(u)] = 0; // BAD
67+
}
68+
69+
void test_alloc() {
70+
// Special case of taking sizeof without any addition or multiplications
71+
var_buf *b = malloc(sizeof(var_buf));
72+
b->buf[1] = 0; // BAD
73+
}

0 commit comments

Comments
 (0)