Skip to content

Commit 469f8a6

Browse files
committed
C++: Add more tests for buffer overruns
1 parent 35b7808 commit 469f8a6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdint.h>
2+
#define SIZE (32)
3+
4+
void test_buffer_overrun_in_for_loop()
5+
{
6+
uint8_t data[SIZE] = {0};
7+
for (int x = 0; x < SIZE * 2; x++) {
8+
data[x] = 0x41; // BAD [NOT DETECTED]
9+
}
10+
}
11+
12+
void test_buffer_overrun_in_while_loop_using_pointer_arithmetic()
13+
{
14+
uint8_t data[SIZE] = {0};
15+
int offset = 0;
16+
while (offset < SIZE * 2) {
17+
*(data + offset) = 0x41; // BAD [NOT DETECTED]
18+
offset++;
19+
}
20+
}
21+
22+
void test_buffer_overrun_in_while_loop_using_array_indexing()
23+
{
24+
uint8_t data[SIZE] = {0};
25+
int offset = 0;
26+
while (offset < SIZE * 2) {
27+
data[offset] = 0x41; // BAD [NOT DETECTED]
28+
offset++;
29+
}
30+
}
31+
32+
int main(int argc, char *argv[])
33+
{
34+
test_buffer_overrun_in_for_loop();
35+
test_buffer_overrun_in_while_loop_using_pointer_arithmetic();
36+
test_buffer_overrun_in_while_loop_using_array_indexing();
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)